Jayel Posted March 17, 2011 Share Posted March 17, 2011 Hi, I have coded a lot in the past but only now getting into PHP and web coding in general, when I use a while loop in most other languages I need to increment some form of counter to move to the next item e.g. while (count < 10) { Do something; count + 1; } I am going through a set of tutorials that have just enlightened me to the following code when looking up a MYsql d'base: $reults = "SELECT * FROM table"; while ($row = mysql_fetch_assoc($results)){ extract($row); echo $tblcol1; echo " - "; echo $tblcol2; echo "<br>"; } Now in fairness I have no real problem with this, if thats how you extract your results from a query then fine but I feel I am missing something and have not been able to nail it on the head. Am I to assume that $row in the loop returns the first row from the query results, moves through the actions of the loop, returns then automatically points to the next row of the array? Does that mean while($row){} where $row is a 3x3 array(for example) it would loop through this 3 times assigning the next row until the end? Is there a better way to explain this? Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/ Share on other sites More sharing options...
Sleeper Posted March 17, 2011 Share Posted March 17, 2011 I do belive if you want to loop all the info you would do $reults = "SELECT * FROM table"; while ($row = mysql_fetch_assoc($results)){ echo ' '.$row[tblcol1].' - '.$row[tblcol2].' <br>'; } This would then loop every col1 and col2 option. like.. A1 - A2 B1 - B2 C1 - C2 Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188786 Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 Am I to assume that $row in the loop returns the first row from the query results, moves through the actions of the loop, returns then automatically points to the next row of the array? Basically, from the manual: // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188788 Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 And when posting code please use tags. Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188791 Share on other sites More sharing options...
Jayel Posted March 17, 2011 Author Share Posted March 17, 2011 Hi, Thanks for the quick reply, I will remember the code tags from now on. So you are saying that PHP automatically increments through each item in $row? Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188793 Share on other sites More sharing options...
Sleeper Posted March 17, 2011 Share Posted March 17, 2011 When you use the "while" part yes. Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188795 Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 More like each row in a result set. It sets $row to the next row (assuming it exists) which is actually an associative array when using mysql_fetch_assoc. Quote Link to comment https://forums.phpfreaks.com/topic/230941-while-loop-auto-increment/#findComment-1188797 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.