Current Location: Home> Latest Articles> Detailed explanation of the use and parameters of mysqli_result::fetch_array()

Detailed explanation of the use and parameters of mysqli_result::fetch_array()

M66 2025-05-17

In PHP, if you use the (MySQL Improved Extension) extension to connect to the database and execute the query, when you execute a query function like mysqli_query() , if the query returns a result set (such as a SELECT query), you will get a mysqli_result object.

This mysqli_result object is an interface for manipulating query results, through which you can read the data returned by the query line by line and field by field.

What is mysqli_result?

mysqli_result is a result set object that exists in both object-oriented and procedural styles. It contains all rows and column data returned by the database query, but it does not load all of these data into memory at once, but needs to be fetched on demand through various methods.

Common ways to obtain data are:

  • fetch_assoc() → Get a row of associative arrays

  • fetch_row() → Get an index array of rows

  • fetch_object() → Get a row of objects

  • fetch_array() → Get a row, either an associative array, an index array, or both (configurable)

This article focuses on fetch_array() .

How to use the fetch_array() method?

fetch_array() is a method of mysqli_result , used to obtain a row of data in the current result set, and the return form is an array.

There are two styles of calling:

Object-oriented style:

 $result = $mysqli->query("SELECT id, name FROM users");
$row = $result->fetch_array();

Process style:

 $result = mysqli_query($conn, "SELECT id, name FROM users");
$row = mysqli_fetch_array($result);

Either way, it takes one line every time it calls until there are no more lines (returns false at this point).

What do the parameters mean?

fetch_array() can receive an optional parameter to control the type of the return array.
Full signature:

 array mysqli_fetch_array(mysqli_result $result, int $resulttype = MYSQLI_BOTH)

The parameter $resulttype value is:

  • MYSQLI_ASSOC → Return only associative array (field name as key)

  • MYSQLI_NUM → Return only the number index array (field index is key)

  • MYSQLI_BOTH (default) → Returns the association + numeric index mixed array

For example:

 $conn = mysqli_connect("localhost", "username", "password", "database");
$result = mysqli_query($conn, "SELECT id, name FROM users");

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo "ID: " . $row['id'] . " Name: " . $row['name'] . "<br>";
}

Here we use MYSQLI_ASSOC , so there is only the field name key in $row , and there is no numeric index.

Example code

Complete example: