Difference between mysqli_fetch_array() & mysqli_fetch_object() in PHP
Last Updated :
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.
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+ |
Similar Reads
Difference between mysql_connect() and mysql_pconnect() Functions in PHP
mysql_connect() Function: The mysql_connect() function is used to establish a new connection with the database. This connection is established when the script starts its execution. After establishing this connection with the database, it will be valid or be connected with the database only until the
3 min read
Difference between Array and Array of Objects in JavaScript
ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
What is the difference between lists and arrays?
In programming, lists and arrays are data structures used to organize and store data. Both have their unique features and purposes. Lists are dynamic and flexible, allowing for easy resizing during runtime, while arrays are static with a fixed size. This difference impacts memory usage and performan
8 min read
What is the difference between for and Foreach loop in PHP ?
Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the ca
3 min read
Difference between array_merge() and array_combine() functions in PHP
array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the e
3 min read
Difference Between JavaScript Arrays and Objects
Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p
1 min read
What is the difference between eq() and get() methods in jQuery ?
In this article, we will discuss all the differences between eq() and get() methods in jQuery. eq() Method: This method is used to locate the selected elements directly and returns an element with a specific index. Syntax: $(selector).eq(index) Example: In this example, we will set the different te
2 min read
Difference between die() and exit() functions in PHP
PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
2 min read
Difference between Array and String in Java
An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
Difference between isset() and empty() Functions
The isset() and empty() functions in PHP are used for variable checking but serve different purposes. isset() checks if a variable is set and not null, while empty() checks if a variable is considered empty, like 0, false, null, or an empty string.Table of Contentempty() Functionsisset() FunctionDif
5 min read