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
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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