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? Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/ 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. Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/#findComment-480486 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? Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/#findComment-480488 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). Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/#findComment-480493 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. Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/#findComment-480503 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 Link to comment https://forums.phpfreaks.com/topic/93768-mysql_fetch_object-confusion/#findComment-480510 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.