PHP mysqli_fetch_object() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

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);
CallWhat 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->nameReads 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.

Library declarations
PHP
mysqli_fetch_object(mysqli_result $result, string $class = "stdClass", array $constructor_args = []): object|null|false

Parameters

ParameterDescription
resultRequired. A mysqli_result with unread rows.
classOptional. Class name to instantiate. Defaults to stdClass.
constructor_argsOptional. Arguments passed to the class constructor as an array.

Return Value

Return ValueDescription
object|null|falseAn 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);

How It Works

  1. The SELECT returns one product row.
  2. mysqli_fetch_object() builds a stdClass with id and name properties.
  3. Arrow syntax reads those properties.
  4. 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);

How It Works

  1. A Product class declares public id and name properties.
  2. mysqli_fetch_object() creates a Product and fills those properties.
  3. get_class() confirms the instance type.
  4. 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);

How It Works

  1. Each iteration fetches the next object row.
  2. null stops the loop at the end of the result.
  3. Only the name property is printed.
  4. 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.

Next: mysqli_fetch_row()

mysqli_fetch_row() reads the next row from a mysqli_result and returns it as a numeric arr.

mysqli_fetch_row() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful