1

I'm a beginner C programmer. I recently started learning PHP and MySQL and encountered this interesting behavior with while loop:

while ($pages = mysql_fetch_array($pages_set))
    {statement}

I previously learned that condition must be changed somewhere in the loop (or in the expression itself) in order for the loop to be finite, but in this case i just can't see it.

I researched a bit about mysql_fetch_array() function and found this:

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Is it true to say that the pointer moves through the row and when it reaches the end it will return 0 or NULL?

1
  • since you're just learning PHP, it's worth pointing out that the mysql_xxx() functions are deprecated. You should use either the mysqli_xxx() alternatives or the PDO library instead. See the PHP manual for more info. However, with either of these libraries, similar constructs are possible, so your question is still relevant. Commented Jul 31, 2012 at 19:46

1 Answer 1

4

The pointer moves to the next row in the recordset with each iteration, so that when there is no data left then $pages will be NULL and the loop will end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.