I have troubles to understand the use of
while($row = mysql_fetch_array($query))
to display some elements of a database that are in the variable “$query”.
Moreover, I dont understand what is passed to the variable “$row” by the function “mysql_fetch_array()”.
My guess is that only one record (or row) of the database records that are in “$query” is given, but how does the function know which row??
I’ll try to explain myself using an example.
Assuming I have 3 records in the variable $query with two fields (say “id” and “name”).
At the first while the variable $row is filled by the first element (say id=1, name=antonio), then some code allow me to display or manipulate “1” and “antonio”, then the code ends and the while starts again.
Now I assume that what is given to the variable “row” is the second element, but how does the function knows that must return the second element (and not the first one or the third one)?? I mean, I dont give this information anywhere…
The variable $query is a resource and as such, has some sort of internal “row counter”. Every time you call mysql_fetch_array() or a variant thereof, this counter gets incremented. As a result, every time you call this function mysql_fetch_array, you get the next row. When the last row has been received, and you call it again, it returns false. Because of the way a while loop works, you break out of this loop.