Page 1 of 1

Have problem with while ($row=mysql_fecth_assoc($resultprop)

Posted: Thu Feb 22, 2007 12:31 pm
by hamzatki
The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

I have problem using while() inside a For Loop in the code below.

Code: Select all

<?php



$allprop_id = explode(",", $prop_id1);

for($i = 0; $i < $selected; $i++)

{

$test = $allprop_id[$i];

echo "$test <br />"; 

// [color=red]displaying the correct values of $test for the current values of $i[/color]

$sqlprop = "SELECT * FROM property where prop_id ='$test'";
$resultprop = mysql_query($sqlprop);

while ($row=mysql_fetch_assoc($resultprop))

{
$email=$row["$email"]; $prop_id = $row["$prop_id"] ...........;

$sqlfn_new = "INSERT INTO enquiry (email, prop_id, ......) VALUES('$email, '$prop_id', ......)";

$resultfn_new = mysql_query($sqlfn_new);

// [color=red]inserting the same first values next subsequent values of $i [/color]

echo "$test <br />"; 

// [color=red]keep on displaying the same value of $test for $i = 1[/color]
}

echo "$test <br />";

// [color=red]outside the while loop displaying the correct values of $test for the current values of $i[/color]


}

?>
The impression I had was that the pointer was not return to the top and when I used mysql_data_seek($resultprop, 0), it dispalyed error "Offset 0is invalid for Mysql result iondex 16 (or the query data is unbuffered)".

Please I need your help.


The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Feb 22, 2007 1:28 pm
by volka
please try

Code: Select all

$sqlprop = "SELECT * FROM property where prop_id ='$test'";
$resultprop = mysql_query($sqlprop) or die(mysql_error().': '.$sqlprop);
echo '<p># result for "', $sqlprop, '": ', mysql_num_rows($resultprop), "</p>\n";
while ($row=mysql_fetch_assoc($resultprop))

Posted: Thu Feb 22, 2007 5:19 pm
by hamzatki
Hi Volka,

Thanks for your contribution.

These are the results i got after:

There were 2 iterations and

the first echo for " echo '<p># result for "', $sqlprop, '": ', mysql_num_rows($resultprop), "</p>\n"; "

gave this. As i have said earlier the prop_id is correct, and if you noticed mysql_num_rows($resultprop) yielded 1.

# result for "select * from property where prop_id ='funmi@ayedun.com1170175019'" : 1

Also for the second iteration the prop_id is correct, but mysql_num_rows($resultprop) yielded 0.

# result for "select * from property where prop_id ='funmi@ayedun.com1170175006'" : 0

When I checked the table, it inserted the first prop_id twice.

I think it is worthwhile to find out why mysql_num_rows($resultprop) for the 2nd iteration was given 0.

Posted: Thu Feb 22, 2007 5:36 pm
by volka
hamzatki wrote:I think it is worthwhile to find out why mysql_num_rows($resultprop) for the 2nd iteration was given 0.
Because no record was selected because there's no record matching the WHERE clause.
hamzatki wrote:When I checked the table, it inserted the first prop_id twice.
Did you run the script twice?