Jump to content

mysql_fetch_object() confusion


frijole

Recommended Posts

<?php
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
}
mysql_free_result($result);
?>

 

In this code the mysql query is stored in the variable $result. Then, the while loop begins and its condition is:

while ($row = mysql_fetch_object($result)) 

 

$row was never set to TRUE. mysql_fetch_object($result) returns TRUE while there are still more rows matching the query, I think. So, how does this work? Am I missing something?

Link to comment
https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/
Share on other sites

"something" can be any expression returning a true or false result. As long as the expression evaluates to TRUE the loop executes

 

When converting to boolean, the following values are considered FALSE:

 

    * the boolean FALSE itself

    * the integer 0 (zero)

    * the float 0.0 (zero)

    * the empty string, and the string "0"

    * an array with zero elements

    * an object with zero member variables (PHP 4 only)

    * the special type NULL (including unset variables)

    * SimpleXML objects created from empty tags

 

Every other value is considered TRUE (including any resource).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.