Jump to content

mysql_fetch_row()


livethedead

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.