The mysqli_fetch_object()
function in PHP is used to fetch a result row as an object from a MySQL database query result. Each row is returned as an object with properties corresponding to the field names of the result set.
Syntax:
mysqli_fetch_object($result);
$result
: The result set returned from a MySQL query (typically frommysqli_query()
ormysqli_store_result()
).
Return Value:
- It returns an object that represents the row fetched. Each column in the row is accessible as a property of the object.
- If there are no more rows, it returns
NULL
.
Example 1: Basic Example with mysqli_fetch_object()
Here’s an example of how to use mysqli_fetch_object()
to fetch rows from a users
table:
<?php
// Connect to MySQL
$connection = mysqli_connect("localhost", "root", "", "test_db");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Query the database
$query = "SELECT id, name, email FROM users";
$result = mysqli_query($connection, $query);
// Check if any rows were returned
if (mysqli_num_rows($result) > 0) {
// Fetch each row as an object
while ($row = mysqli_fetch_object($result)) {
// Access columns as object properties
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->name . "<br>";
echo "Email: " . $row->email . "<br><br>";
}
} else {
echo "No records found!";
}
// Close the connection
mysqli_close($connection);
?>
Explanation:
- Connection: We connect to the MySQL database with
mysqli_connect()
. - Query: We execute a
SELECT
query to retrieve user data from theusers
table. mysqli_fetch_object()
: In thewhile
loop, we usemysqli_fetch_object()
to fetch each row as an object. We access the columns using->
syntax (e.g.,$row->id
,$row->name
,$row->email
).
Example 2: Using Custom Class for Objects
You can also use a custom class with mysqli_fetch_object()
. If the result set matches the properties of the class, it will be mapped to that class.
<?php
class User {
public $id;
public $name;
public $email;
}
// Connect to MySQL
$connection = mysqli_connect("localhost", "root", "", "test_db");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Query the database
$query = "SELECT id, name, email FROM users";
$result = mysqli_query($connection, $query);
// Fetch each row as an object of the User class
while ($user = mysqli_fetch_object($result, 'User')) {
echo "ID: " . $user->id . "<br>";
echo "Name: " . $user->name . "<br>";
echo "Email: " . $user->email . "<br><br>";
}
// Close the connection
mysqli_close($connection);
?>
Explanation:
- Custom Class: We define a
User
class with public properties (id
,name
,email
). - Mapping: In
mysqli_fetch_object()
, we specify the class name ('User'
), and the fetched row is mapped to an object of that class.
Key Points:
- The columns are accessed as object properties.
- It is useful when you prefer to work with objects rather than associative arrays.
- It can be combined with custom classes to map rows to more structured objects.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.