mysql_fetch_object() Function in PHP

What is mysql_fetch_object() function in PHP?
How does mysql_fetch_object() works?


This mysql_fetch_object() function fetches a query result row and return as an object.
object mysql_fetch_object ( resource result)

Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.
mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets.
mysql_fetch_object() function retrieves a row of data from a result returned by mysql_db_query() or mysql_query() and return as an object, you can retrieve the field value with the field name instead of field index. Each subsequent call to mysql_fetch_object() returns the next row in the recordset.
The data is returned as an object.

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "n";
// Simple Select query
$query = "SELECT * FROM my_table";
$result = mysql_query($query);
//Fetches row of result set as an object
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;

The output of above code is given below
1 jim
2 kiet
3 john

See also:
mysql_fetch_array(), mysql_fetch_assoc() and mysql_fetch_row()

PHP Topics

Ask Questions

Ask Question