Open In App

Difference between mysqli_fetch_array() & mysqli_fetch_object() in PHP

Last Updated : 28 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the  mysqli_fetch_array() & mysqli_fetch_object() function in PHP. The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

mysqli_fetch_object() function: This function returns an object with properties that match the fetched row’s properties and advances the internal data pointer. We can only access the data by the specific field names, and not by their offsets. It returns an object containing properties that match the fetched row, or FALSE if no further rows are available.

Syntax:

mysqli_fetch_object(data);

Parameters:

  • data: The data resource that is being evaluated. This result comes from a call to mysqli_query() function.

Example: This example describes the mysqli_fetch_object() function that fetches the next row of a result set as an object.

Suppose we have table named students which have the following data values:

Create table:

CREATE TABLE students (
    student_id int(10),
    student_name VARCHAR(20)
)

Insert into the table:

Insert into students(
    student_id, student_name
) Values(01, 'Abc')

Insert into Students(
    student_id, student_name
) Values(02, 'Xyz')

After creating the table, the following table structure will appear.

student_id student_name
01 Abc
02 Xyz

PHP




<?php
 
$con = mysqli_connect("localhost", "root",
    "", "mydb") or die(mysqli_error($con));
 
$select_query = "SELECT * FROM students";
 
$select_query_result = mysqli_query($con,
    $select_query) or die(mysqli_error($con));
 
while ($row = mysqli_fetch_object($select_query_result)) {
    echo $row->student_id;
    echo " ";
    echo $row->student_name;
}
 
mysqli_free_result($select_query_result);
 
?>


Output:

01 Abc
02 Xyz

mysqli_fetch_array() function: It is used to fetch rows from the database and store them as an array. The array can be fetched as an associative array, as a numeric array, or both.

Syntax:

mysqli_fetch_array(result, arrayType);

Parameters:

  • result: The result of resource that is being evaluated. This result comes from a call to mysqli_query() function.
  • arrayType: The type of array that is to be fetched. It’s a constant and can take the following values: MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.

Example: This example describes the mysqli_fetch_array() function that returns an array that corresponds to the fetched row & moves the internal data pointer ahead.

Suppose we have table named students which have the following data values:

Create table:

CREATE TABLE students (
    student_id int(10),
    student_name VARCHAR(20)
)

Insert into the table:

Insert into students(
    student_id, student_name
) Values(01, 'Abc')

Insert into Students(
    student_id, student_name
) Values(02, 'Xyz')

After creating the table, the following table structure will appear.

student_id student_name
01 Abc
02 Xyz

PHP




<?php
 
$con = mysqli_connect("localhost", "root",
    "", "mydb") or die(mysqli_error($con));
 
$select_query = "SELECT * FROM students";
 
$select_query_result = mysqli_query($con,
    $select_query) or die(mysqli_error($con));
 
while ($row = mysqli_fetch_array($select_query_result)) {
    echo $row['student_id'] . " " . $row['student_name'];
}
 
mysqli_free_result($select_query_result);
 
?>


Output:

01 Abc
02 Xyz

Difference between mysqli_fetch_array() and mysqli_fetch_object() Function:

mysqli_fetch_object() function: Fetch a result row as an object.

mysqli_fetch_array() function: Fetch a result row as a combination of associative array and regular array.

Let us see the differences in a tabular form -:

  mysqli_fetch_array() mysqli_fetch_object() 
1. It fetches a result row as an associative array and a numeric array. It is used to return the row of a result-set
2.

Its syntax is -:

$mysqli_result -> fetch_array(result_type)

Its syntax is -:

$mysqli_result -> fetch_object(classname, params)

3. It takes one parameter that is a result. It takes two parameters that are class name and array of parameters
4. Its return value is array of string. It returns an object with string properties for the fetched row.
5. It is supported in PHP version 5+ It is supported in PHP version 5+


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads