problems with WHILE LOOPS!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

problems with WHILE LOOPS!!!!

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply