Jump to content

While loop problem!


jblast3000

Recommended Posts

Hi, I'm using a while loop to print each row of a mqsql database table:

while($row = mysql_fetch_array($data) or die(mysql_error())){
print $row['name'] ."<br/>";
}

 

This code works grate, except none of my code after the while loop executes. Is there something wrong with the while loop? (I didn't get any errors)

 

NOTE:

a while loop like the following doesn't have this problem:

$i = 0;
while($i < 10){
print $i;
$i++;
}

Link to comment
Share on other sites

mysql_fetch_array returns false when it is done executing, thus triggering the die situation. but there is no error to display

while($row = mysql_fetch_array($data) or die("Error: ".mysql_error())){
print $row['name'] ."<br/>";
}

 

to see yer code in action

what shud be done is

 

$data=mysql_query("SELECT * from users") or die("MySQL Error:". mysql_error());
while($row = mysql_fetch_array($data)){
print $row['name'] ."<br/>";
}

 

now if u need to know if it returned any rows before your loop

$data=mysql_query("SELECT * from users") or die("MySQL Error:". mysql_error());
if(!mysql_num_rows($data))
  echo "No Info";
else {
while($row = mysql_fetch_array($data)){
print $row['name'] ."<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.