简体   繁体   中英

A While Loop Condition In PHP

I have seen the following while loop condition in many examples and even I have used it in many times. I know how it works and how to use it. But as i code the condition here doesn't make any sense.

As I see the code, the condition is like it's always true. Just like while(1) . Because to the *mysql_fetch_assoc()*, the same data is passed is passed all the time. So the condition is a constant.

while($arr = mysql_fetch_assoc($data)) 
{
      //other code
}

Now, where Am I Wrong ????

Each call to mysql_fetch_assoc gets the next row from the result set. If there is no row anymore it returns false and the loop terminates.

$data is a resource data type and will probably keep the state about which row was fetched last.
This is not so unusual, even arrays have an internal pointer to the current element which can be manipulated using certain array functions.

From the PHP website , mysql_fetch_assoc " Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. "

Therefore, whenever there are still rows available, mysql_fetch_assoc will return an associated array of the row contents, will increment the pointer to the next row and the while loop will execute.

When there are no more rows mysql_fetch_assoc will return FALSE and the loop will break.

Also note that as of PHP 5.5.5 this function will be deprecated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM