Difference between mysqli_fetch_array() & mysqli_fetch_object() in PHP

28 Apr, 2022
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.
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.
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 -:
Its syntax is -:
$mysqli_result -> fetch_array(result_type)
Its syntax is -:
$mysqli_result -> fetch_object(classname, params)
mayank007rawa
PHP-function
PHP-Questions
Picked
Difference Between
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Compile-time and Run-time Polymorphism in Java
Similarities and Difference between Java and C++
Difference between Internal and External fragmentation
How to execute PHP code using command line ?
PHP in_array() Function
How to delete an array element based on key in PHP?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?