Page 1 of 1

problems with WHILE LOOPS!!!!

Posted: Mon Apr 21, 2003 4:58 pm
by Jade
I just switched to a new host service and now evey time i run a while loop i get error message. This is my while loop:

$count = 0;

$result = mysql_query("SELECT mid FROM members WHERE logged_on='1'")
or die ("cannot select horse name");

$mid = mysql_result($result,$count,"mid");

while ($mid != "") //member id
{

$result = mysql_query("SELECT name FROM members WHERE mid='$mid'")
or die ("cannot select horse name");

$name = mysql_result($result,0,"name");

//link to that member's page goes here

$result = mysql_query("SELECT mid FROM members WHERE logged_on='1'")
or die ("cannot select horse name");

$mid = mysql_result($result,$count,"mid");

$count++;

}

But when i run this script i get the following warning:

Warning: Unable to jump to row 3 on MySQL result index 12 in /home/whiteoak/public_html/english/updates.php on line 252

Line 252 is the one I've highlighted in red. Can anyone help me? I've tried changing my loop and still no success...

Jade

Posted: Mon Apr 21, 2003 7:57 pm
by Jade
I'll leave this as a refrence for other people. I did figure out how to fix this problem even though there is really nothing wrong with my earlier script. I had to switch the script to an array, it was the only way I could get the error message to go away.

This is what it was changed to:

$result = mysql_query("SELECT mid, name FROM members WHERE logged_on='1'")
or die ("cannot select horse name");

while ($row = mysql_fetch_array($result))
{
$mid = $row["mid"];
$name = $row["name"];

//link to that member's page goes here
<a href=visit_home.php?mid=<?php echo $mid; ?>>
<?php echo $name; ?>
</a>

}

Just thought I'd give you a heads up and help anyone else who ever runs into this problem in the future.

Jade

Posted: Tue Apr 22, 2003 2:47 am
by twigletmac
The code you've now got is better anyway, mysql_fetch_xxx() should be used in preference to mysql_result() when you're looping through a result set and one query is generally preferable to two (especially when one is in a while loop and thus the database is being queried twice for each record).

Mac