livethedead Posted February 22, 2012 Share Posted February 22, 2012 Alright, I have a question regarding mysql_fetch_row. The manual says: Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows. However, <?php $array = mysql_query(); $data = mysql_fetch_row($array); while ($data) { dosomething } Turns into an endless loop. I'm sure I'm missing something and I'm just looking for an explanation. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/ Share on other sites More sharing options...
livethedead Posted February 22, 2012 Author Share Posted February 22, 2012 I named mysql_query wrong, but my question still applies. Don't get confused by that. Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/#findComment-1320126 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 while( $data = mysql_fetch_row( . . . ) ) { // do something } Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/#findComment-1320127 Share on other sites More sharing options...
livethedead Posted February 22, 2012 Author Share Posted February 22, 2012 Ah, I see. I don't quite understand how there is a difference between the two though. I take that back, after looking at it I understand. Appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/#findComment-1320128 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 The difference is that when you call mysql_fetch_row() it advances an internal pointer to the next row. If there isn't another row, it returns false. So basically in the while statement as soon as you have advanced through all of the rows, the statement is false and thus the while loop fails. Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/#findComment-1320132 Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 Okay. I'll break this down a bit. Let's start with your code: <?php $array = mysql_query(); $data = mysql_fetch_row($array); while ($data) { dosomething } now, by declaring $data = mysql_fetch_row($array) outside of the while loop, you are setting the value of $data to a numerical array of strings that correspond to the fetched row, and this value will be static since it is outside of the while loop. the while loop that you have checks for that value to be true, which it always will be since it is a valid array. The reason why you specify: while($data = mysql_fetch_row()) inside of the while loop is because the nature of mysql_fetch_row(). I will quote from the manual here: Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. this means that when mysql_fetch_row() is called, the internal pointer is moved forward to the next results set row. So: while($data = mysql_fetch_row()) will check for mysql_fetch_row() to be true every iteration of the loop. Once it returns false (there are no more rows to return) the loop is stopped. Quote Link to comment https://forums.phpfreaks.com/topic/257561-mysql_fetch_row/#findComment-1320140 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.