frijole Posted February 29, 2008 Share Posted February 29, 2008 <?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? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 29, 2008 Share Posted February 29, 2008 As long as there is a next row to fetch, $row is set to an object containing the row's field values. At the end, when there are no more rows, mysql_fetch_object returns FALSE instead of an object. Quote Link to comment Share on other sites More sharing options...
frijole Posted February 29, 2008 Author Share Posted February 29, 2008 I am confused as to what the condition of the while loop is, i.e. while (something) the following code is executed. What is the something? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 29, 2008 Share Posted February 29, 2008 "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). Quote Link to comment Share on other sites More sharing options...
frijole Posted February 29, 2008 Author Share Posted February 29, 2008 ok, now that makes sense. So, $row = mysql_fetch_object($result) is not false (true) until it runs out of rows. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 29, 2008 Share Posted February 29, 2008 Exactly. Same goes for mysql_fetch_row mysql_fetch_array mysql_fetch_assoc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.