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
https://forums.phpfreaks.com/topic/92225-while-loop-problem/
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
https://forums.phpfreaks.com/topic/92225-while-loop-problem/#findComment-472453
Share on other sites

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.