pandu Posted July 7, 2010 Share Posted July 7, 2010 Hi, I am getting confused by what seems like some kind of shortcut version of PHP, especially when using loops. For example, in the code below, the while loop is apparently going thru the results of a query. But the structure of the loop makes no sense to me. Issue 1 - Where did $result_row come from? OK, I get it that PHP allows on the fly variable declaration. But $result_row has not even been assigned a value, so how can it be used to do a compare (as is being done below inside the condition part of the loop)? Issue 2 - mysql_fetch_row(($result)) is supposed to output the first row of a query - right? So what makes it go to the next part of the query result when the loop runs the second time? Lets assume that $result = [A, B, C, D]. Then mysql_fetch_row($result) will always be A, no matter how many times the loop runs. But in reality it is iterating thru the result. How is that possible? Issue 3 - Is there a more intuitive way to write the while loop so that it makes sense instead of the shortcut version? While (end of array is not reached) { Do stuff; } $query = "SELECT * FROM 'books' NATURAL JOIN 'authors'"; $result = mysql_query($query); i=0; while ($result_row = mysql_fetch_row(($result))) { echo $result_row[i].'<br/>'; i++; } TIA Quote Link to comment https://forums.phpfreaks.com/topic/207060-shortcut-php/ Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 Issue 1 - Where did $result_row come from? OK, I get it that PHP allows on the fly variable declaration. But $result_row has not even been assigned a value, so how can it be used to do a compare (as is being done below inside the condition part of the loop)? It is NOT being used as a compare it is doing an assignment. That if condition will return TRUE if $result_row can be assigned a value from the mysql_fetch_row() function. Each time that function is called the next record will be retrieved and - in this instance - assigned to $result_row. So, once the last record from the result set is used a new value is not assigned to $result_row and the condition is false. Issue 2 - mysql_fetch_row(($result)) is supposed to output the first row of a query - right? So what makes it go to the next part of the query result when the loop runs the second time? Lets assume that $result = [A, B, C, D]. Then mysql_fetch_row($result) will always be A, no matter how many times the loop runs. But in reality it is iterating thru the result. How is that possible? Same answer as above - as each record is extracted from the result set, the pointer moves to the next record in the result set. You should take a few minutes and read the manual. Issue 3 - Is there a more intuitive way to write the while loop so that it makes sense instead of the shortcut version? While (end of array is not reached) { Do stuff; } Huh?! It makes prefect sense. Continue assigning the next record to $result_row until there are no more records left. Again, read the manual. What doesn't make sense in that code is the use of $i. The way it is used it will show the first field from the first record, the second field from the second record, etc. Quote Link to comment https://forums.phpfreaks.com/topic/207060-shortcut-php/#findComment-1082714 Share on other sites More sharing options...
AbraCadaver Posted July 7, 2010 Share Posted July 7, 2010 1. It is not comparing == it is actually making the assignment = and then evaluating it to true or false. After the last row it will return false and the loop will stop. 2. No, it advances to the next row. That's how it works. The manual says "Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. " 3. This is very common and I've never heard of it called a shortcut. Quote Link to comment https://forums.phpfreaks.com/topic/207060-shortcut-php/#findComment-1082718 Share on other sites More sharing options...
pandu Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks guys. Makes perfect sense now. I am reading this O'reilly book. I'll read the PHP manual too now. Quote Link to comment https://forums.phpfreaks.com/topic/207060-shortcut-php/#findComment-1082720 Share on other sites More sharing options...
Andy-H Posted July 7, 2010 Share Posted July 7, 2010 $query = "SELECT * FROM 'books' NATURAL JOIN 'authors'"; $result = mysql_query($query); while ($result_row = mysql_fetch_row(($result))) { echo implode(', ', $result_row) . '<br/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/207060-shortcut-php/#findComment-1082721 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.