This is a manual to learn about the PHP mysqli_fetch_array
function. It describes the syntax, parameters, return values, and some usage practices of this method.
It is an unskippable function when building database-related modules.
The mysqli_fetch_array
is a pre-built function in PHP. This is for fetching a row from the database result set. Each time calling this function, it returns the next row from the result set subsequently.
public mysqli_result::fetch_array(int $mode = MYSQLI_BOTH): array|null|false
mysqli_fetch_array(mysqli_result $result, int $mode = MYSQLI_BOTH): array|null|false
The mysqli_result::fetch_array
and mysqli_fetch_array
gives the same results. It varies in the style of calling by object-oriented or procedural methods.
The $result is the prime reference used to fetch the row which is the object representing the result set. The applicable parameters and return values of these functions are listed below.
$result
– It is a database resource object that represents the result set. The result set is created by PHP mysqli_query. This argument applies to the procedural style only. In the object-oriented style the fetch_array
is invoked upon this $result
object.
$mode
– It is for specifying what type of array (numeric, associative, or both) to be returned. The possible values are the PHP MYSQL constants MYSQLI_NUM
, MYSQLI_ASSOC
and MYSQL_BOTH
(default). It is an optional parameter.
This method returns a row from the result set. It returns null if no more rows in the result set. If the fetch process is failed, then it will return false.
This tutorial gives two examples of fetching the database rows via both the procedural and object-oriented styles.
Both examples print the resultant array using the PHP print statement. You can replace the print statement to handle the row data to display in a table view or to perform any manipulations.
<?php
// Create a MySQL database connection
$connection = mysqli_connect("localhost", "root", "", "db_profile");
$query = "SELECT * FROM tbl_user";
// Get result set
$result = mysqli_query($connection, $query);
// Fetching next row from the result set using mysqli_fetch_array()
$row = mysqli_fetch_array($result);
print "<PRE>";
print_r($row);
mysqli_free_result($result);
// Closing connection
mysqli_close($connection);
?>
<?php
$mysqli = new mysqli("localhost", "root", "", "db_profile");
$query = "SELECT * FROM tbl_user";
$result = $mysqli->query($query);
$row = $result->fetch_array();
print "<PRE>";
print_r($row);
?>
The resultant row is an array with both the field offset and field names as its indices.
The numerical indices are useful at the time of accessing multiple columns with the same name without overriding. The below image shows the database rows and the mysqli_fetch_array()
function’s return array. The output array contains both the numeric and associative array elements.
In the above PHP examples, it prints the array result of mysqli_fetch_array()
.
This PHP example shows how to print the row returned by mysqli_fetch_array()
. It prints the row data as comma-separated values by running this function in a PHP loop.
<?php
// Create a MySQL database connection
$connection = mysqli_connect("localhost", "root", "", "db_profile");
$query = "SELECT * FROM tbl_user";
// Get result set
$result = mysqli_query($connection, $query);
// Fetching rows in a loop
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
print implode(", ", $row) . "<br/>";
}
mysqli_free_result($result);
// Closing connection
mysqli_close($connection);
?>
Output:
1, Kevin, Thomas, Google
2, Tim, Martin, Tesla
PHP provides a function mysqli_fetch_all()
to get all the rows in one call. But, it is not a good idea if the database is large.
Memory efficiency-wise mysqli_fetch_array()
is the best compared to the mysql_fetch_all()
. The latter will affect the performance if the fetch is dealt with the large record set.
The resultant array can be filtered further by applying PHP conditions inside the loop.
PHP fetch_array
function results in the same array as the mysqli_fetch_array()
. The difference is that it is an object-oriented way of fetching the database rows.
It needs the MySQLi
instance to be created with the database credentials as shown in the below example. Then, that instance is used to get the result set. The PHP fetch_array
is called upon this result set object.
By adding the ORDER BY clause to the query, it will fetch the sorted results from the database
The mysql_
extension is very old and deprecated as of the version PHP 5.5.0. Then, the backward compatibility is also removed in the later version 7.0.0.
The mysqli_fetch_array
is the replacement of this deprecated mysql_fetch_array function.
The PDOStatement::fetch() is also a way to fetch the next row from the result.