Jump to content

Am I an idiot, or is PHP an idot?


johnsmith153

Recommended Posts

If I do this:

 

(1)connect, perform mysql etc.

(2)

while($row = mysql_fetch_array($mysqlRes)) {
echo $row[0]."<br>";
}

everything works

 

However, if after performing mysql etc. I try to do this

while($row = mysql_fetch_array($mysqlRes)) {
echo $row[0]."<br>";
}
while($row = mysql_fetch_array($mysqlRes)) {
echo $row[0]."<br>";
}

//ie perform twice in row - it performs first, but then won't perform second.

 

Is this crazy??

 

How can I get this to work?

Link to comment
https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/
Share on other sites

When the first while loop is finished, it's already at the end, so when the second starts, there is nothing to show.  Here's an example:

 

$num = 0;
while($num < 10) {
     echo $num."<br />";
     $num++;
}
while($num < 10) {
     echo $num."<br />";
     $num++;
}

 

If the above code executes, it only shows the first while loop, 0 - 9, and when that loop is finished, $num already equals 9, so the second loop doesn't run.

 

This fixes my example:

 

$num = 0;
while($num < 10) {
     echo $num."<br />";
     $num++;
}
$num = 0;
while($num < 10) {
     echo $num."<br />";
     $num++;
}

 

This should fix your problem:

 

$mysqlRes = mysql_query($query);
while($row = mysql_fetch_array($mysqlRes)) {
echo $row[0]."<br>";
}
$mysqlRes = mysql_query($query);
while($row = mysql_fetch_array($mysqlRes)) {
echo $row[0]."<br>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.