jblast3000 Posted February 21, 2008 Share Posted February 21, 2008 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 More sharing options...
bpops Posted February 21, 2008 Share Posted February 21, 2008 when you get to the end of your mysql data, the $row=mysql_fetch_array($data) returns NULL, which means your die() function executes (skipping the rest of your code). Easiest solution is to remove the 'or die()' part. Link to comment https://forums.phpfreaks.com/topic/92225-while-loop-problem/#findComment-472451 Share on other sites More sharing options...
toplay Posted February 21, 2008 Share Posted February 21, 2008 get rid of or die() part Link to comment https://forums.phpfreaks.com/topic/92225-while-loop-problem/#findComment-472452 Share on other sites More sharing options...
laffin Posted February 21, 2008 Share Posted February 21, 2008 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 More sharing options...
jblast3000 Posted February 21, 2008 Author Share Posted February 21, 2008 OK, thanks Makes sense now. Link to comment https://forums.phpfreaks.com/topic/92225-while-loop-problem/#findComment-472455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.