Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Spudley Jul 31 '12 at 19:46
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks! just what i thought... –  Lulu Aug 10 '12 at 11:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.