johnsmith153 Posted June 5, 2009 Share Posted June 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/ Share on other sites More sharing options...
zer0day Posted June 5, 2009 Share Posted June 5, 2009 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/#findComment-849687 Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2009 Share Posted June 5, 2009 Don't wasted time and server resources executing a query again just so that you can access the same result set - http://us2.php.net/mysql_data_seek Quote Link to comment https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/#findComment-849690 Share on other sites More sharing options...
JonnoTheDev Posted June 5, 2009 Share Posted June 5, 2009 Yes, put the pointer back to the beginning of the array for your second loop. Do not run the same query twice as zer0day has suggested. Quote Link to comment https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/#findComment-849833 Share on other sites More sharing options...
Daniel0 Posted June 5, 2009 Share Posted June 5, 2009 By your own given possible answers, you're the idiot mysql_fetch_*() moves the pointer one forward. Eventually the pointer will be at the end in which case those functions will return FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/161002-am-i-an-idiot-or-is-php-an-idot/#findComment-849834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.