mysqli_fetch_array PHP Function

by Vincy. Last modified on April 9th, 2024.

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.

mysqli_fetch_array syntax

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.

Object-oriented style

public mysqli_result::fetch_array(int $mode = MYSQLI_BOTH): array|null|false

Procedural style

mysqli_fetch_array(mysqli_result $result, int $mode = MYSQLI_BOTH): array|null|false

Description

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.

Parameters

$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.

Return values

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.

MySQL Brand Image

PHP mysqli_fetch_array example

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 brand logo

mysqli_fetch_array PHP procedural example

<?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);
?>

mysqli_fetch_array php

fetch_array PHP object-oriented example

<?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);
?>

More about mysqli_fetch_array

  1. What is the use of mysqli_fetch_array ()?
  2. How to print mysql_fetch_array in PHP?
  3. What is the result of Fetch_array in PHP?
  4. Was mysql_fetch_array removed in the 7.0 PHP version?

What is the use of mysqli_fetch_array ()?

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.

How to print mysqli_fetch_array() in PHP?

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.

What is the result of Fetch_array in PHP?

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

Was mysql_fetch_array removed in the 7.0 PHP version?

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.

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page