In PHP, mysqli_fetch_object() reads the next row from a mysqli_result and returns it as an object whose properties are the column names.
By default the class is stdClass. You can pass another class name and optional constructor arguments. When there are no more rows it returns null. The object style is $result->fetch_object($class, $constructor_args).
Syntax
Call mysqli_fetch_object() with the values you need. Start with this line.
PHP
$row = mysqli_fetch_object($result);
Call
What it does
mysqli_fetch_object($result)
Returns the next row as a stdClass object.
mysqli_fetch_object($result, "Product")
Creates a Product instance and fills public properties from columns.
$row->name
Reads the name column as an object property.
Property names match column names (or aliases). Private or protected properties are not filled the way public ones are—prefer public properties or a plain stdClass for beginners.
Optional. Class name to instantiate. Defaults to stdClass.
constructor_args
Optional. Arguments passed to the class constructor as an array.
Return Value
Return Value
Description
object|null|false
An object for the next row, null when finished, or false on failure.
PHP Examples
Example 1 — Fetch a Row as stdClass
Read one product row as an object and print its properties. Output is a fixed sample.
PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products WHERE id = 1");
$row = mysqli_fetch_object($result);
echo $row->id, " ", $row->name, PHP_EOL;
mysqli_free_result($result);
mysqli_close($mysqli);
Output
1 Tea Mug
How It Works
The SELECT returns one product row.
mysqli_fetch_object() builds a stdClass with id and name properties.
Arrow syntax reads those properties.
Sample values are printed for the tutorial.
Example 2 — Use a Custom Class Name
Pass a simple class so each row becomes an instance of that class.
PHP
<?php
class Product {
public $id;
public $name;
}
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products WHERE id = 2");
$product = mysqli_fetch_object($result, "Product");
echo get_class($product), ": ", $product->name, PHP_EOL;
mysqli_free_result($result);
mysqli_close($mysqli);
Output
Product: Notebook
How It Works
A Product class declares public id and name properties.
mysqli_fetch_object() creates a Product and fills those properties.
get_class() confirms the instance type.
The sample name Notebook is printed.
Example 3 — Loop Object Rows
Fetch every remaining row as an object until null.
PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products ORDER BY id LIMIT 3");
while ($row = mysqli_fetch_object($result)) {
echo $row->name, PHP_EOL;
}
mysqli_free_result($result);
mysqli_close($mysqli);
Output
Tea Mug
Notebook
USB Cable
How It Works
Each iteration fetches the next object row.
null stops the loop at the end of the result.
Only the name property is printed.
Three sample products appear in order.
Frequently Asked Questions
It returns the next result row as an object with properties named after the columns, or null when there are no more rows.
fetch_assoc() returns an array. fetch_object() returns an object. The column values are the same.
stdClass. Pass another class name as the second argument if you want a custom type.
Call $result->fetch_object($class, $constructor_args) on a mysqli_result.
Yes. The third parameter is an array of values passed to the class constructor.