mysqli_result fetch_object() Method
The mysqli_result::fetch_object() / mysqli_fetch_object() function fetches one row of data from the result set and returns it as an object, where each property represents the name of the result set's column. Each subsequent call to this function will return the next row within the result set, or null if there are no more rows.
If two or more columns of the result have the same name, the last column will take precedence and overwrite any previous data. To access multiple columns with the same name, mysqli_fetch_row() can be used to fetch the numerically indexed array, or aliases may be used in the SQL query select list to give columns different names.
Syntax
//Object-oriented style public mysqli_result::fetch_object(class, constructor_args) //Procedural style mysqli_fetch_object(result, class, constructor_args)
Parameters
result |
Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). |
class |
Optional. Specify the name of the class to instantiate, set the properties of and return. If not specified, a "stdClass" object is returned. |
constructor_args |
Optional. Specify an array of parameters to pass to the constructor for class objects. |
Return Value
Returns an object representing the fetched row, where each property represents the name of the result set's column. It returns null if there are no more rows in the result set, or false on failure.
Example: Object-oriented style
The example below shows the usage of mysqli_result::fetch_object() method.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; if ($result = $mysqli->query($sql)) { //fetching object array while($obj = $result->fetch_object()) { printf("%s, %d\n", $obj->Name, $obj->Age); } //free the memory associated with the result $result->close(); } //closing the connection $mysqli->close(); ?>
The output of the above code will be similar to:
Marry, 23 Kim, 26 John, 27 Adam, 28
Example: Procedural style
The example below shows the usage of mysqli_fetch_object() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; if ($result = mysqli_query($mysqli, $sql)) { //fetching object array while($obj = mysqli_fetch_row($result)) { printf("%s, %d\n", $obj->Name, $obj->Age); } //free the memory associated with the result mysqli_free_result($result); } //closing the connection mysqli_close($mysqli); ?>
The output of the above code will be similar to:
Marry, 23 Kim, 26 John, 27 Adam, 28
❮ MySQLi Functions Reference