SchweppesAle Posted April 20, 2009 Share Posted April 20, 2009 hi, this is probably a really simple question. Up until now I've just been using the following method in order to loop through a mysql query. mysql_select_db("account6_jo152"); $result = mysql_query("SELECT * FROM blahblah WHERE yadayada = '0'"); while($row = mysql_fetch_array($result)) { /*manipulate some column like so for each entry*/ $row['somecolumn']; } I understand that the mysql_fetch_array() method is storing the query within an array named $row; what confuses me is how this statement is incrementing after each iteration. Shouldn't while statements simply continue to loop the same thing over and over again until the condition no longer holds true? How is it cycling through each element in the array? Link to comment https://forums.phpfreaks.com/topic/154922-solved-php-question-regarding-while-statement/ Share on other sites More sharing options...
rhodesa Posted April 20, 2009 Share Posted April 20, 2009 that is just how mysql_fetch_array() works. it increments on each call. so doing this: $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); will also increment each time http://us3.php.net/mysql_fetch_array Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. Link to comment https://forums.phpfreaks.com/topic/154922-solved-php-question-regarding-while-statement/#findComment-814879 Share on other sites More sharing options...
The Little Guy Posted April 20, 2009 Share Posted April 20, 2009 it will ALWAYS return a value so the while loop takes that as TRUE, the when there are no more results mysql_fetch_array returns FALSE, and your loop ends. Return Values Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works). Link to comment https://forums.phpfreaks.com/topic/154922-solved-php-question-regarding-while-statement/#findComment-814888 Share on other sites More sharing options...
SchweppesAle Posted April 20, 2009 Author Share Posted April 20, 2009 alright, I guess that makes sense. Maybe I'm just not use to declaring variables within the while statement ??? Anyway, thanks for clearing that up. Link to comment https://forums.phpfreaks.com/topic/154922-solved-php-question-regarding-while-statement/#findComment-814910 Share on other sites More sharing options...
SchweppesAle Posted April 20, 2009 Author Share Posted April 20, 2009 that is just how mysql_fetch_array() works. it increments on each call. so doing this: $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); will also increment each time http://us3.php.net/mysql_fetch_array Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. that was actually really helpful. I think I get it now, thanks. Link to comment https://forums.phpfreaks.com/topic/154922-solved-php-question-regarding-while-statement/#findComment-814916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.