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
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>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.