TheStalker Posted September 8, 2011 Share Posted September 8, 2011 Hi everyone, Bit of a random question and im sure there is good reason so if someone could in lighten me that would be great ! With regards to the below code, it seems to just create an infinite loop when you put mysql_fetch_array. Just want to know what wrong with doing this? and why it act in this way ? $sql = mysql_query("SELECT * FROM cities"); $results = mysql_fetch_array($sql); while($row = $results) { echo $row['City']."<br />"; } thanks Guys Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/ Share on other sites More sharing options...
Psycho Posted September 8, 2011 Share Posted September 8, 2011 A while() loop continues as long as the condition is true. In the above code you are simply attempting to assign the value of $results to $row. $results was already defined before the loop and equals the first record from the result set. So that condition is simply reassigning $results to $row on each iteration. You aren't getting any new records so $results is basically a static variable. You might was well write the condition as while($foo = 'bar') { Whereas $sql = mysql_query("SELECT * FROM cities"); while($row = mysql_fetch_array($sql)) { echo $row['City']."<br />"; } The condition in this loop is fetching the next record from the result set and assigning the value to $row. When the result set runs out of records the resulting condition is false and the loop ends. Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/#findComment-1266932 Share on other sites More sharing options...
AbraCadaver Posted September 8, 2011 Share Posted September 8, 2011 You are just assigning the $results array to $row, which is always true if the fetch was successful, so the loop never ends. What you want is: $sql = mysql_query("SELECT * FROM cities"); while($row = mysql_fetch_assoc($sql)) { echo $row['City']."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/#findComment-1266933 Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Because $results = mysql_fetch_array($sql); always equates to true in your loop while($row = $results) Here's how it should be written. <?php $result = mysql_query("SELECT * FROM cities"); while($row = mysql_fetch_assoc($result)) { echo $row['City']."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/#findComment-1266935 Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Wow, loads of the same reply in one go. Well done guys Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/#findComment-1266937 Share on other sites More sharing options...
TheStalker Posted September 9, 2011 Author Share Posted September 9, 2011 Cheers guys, seems simple now Quote Link to comment https://forums.phpfreaks.com/topic/246718-why-does-this-happen-with-mysql_fetch_array/#findComment-1267264 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.