PHP mysqli_fetch_array() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_array() reads the next row from a mysqli_result and returns it as an array. By default the array has both numeric indexes and column-name keys.

Pass MYSQLI_ASSOC for name keys only, MYSQLI_NUM for numbered indexes only, or MYSQLI_BOTH (the default) for both. When there are no more rows it returns null. The object style is $result->fetch_array($mode).

Syntax

Call mysqli_fetch_array() with the values you need. Start with this line.

PHP
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
CallWhat it does
mysqli_fetch_array($result)Returns the next row with both numeric and associative keys (MYSQLI_BOTH).
mysqli_fetch_array($result, MYSQLI_ASSOC)Returns the next row keyed only by column names.
mysqli_fetch_array($result, MYSQLI_NUM)Returns the next row keyed only by column position (0, 1, 2…).

Call mysqli_fetch_array() in a loop until it returns null. Each call advances the result cursor by one row. Prefer MYSQLI_ASSOC when you only need column names, because MYSQLI_BOTH stores every value twice.

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

Parameters

ParameterDescription
resultRequired. A mysqli_result from mysqli_query(), mysqli_store_result(), or a similar call.
modeOptional. MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (default). Chooses which keys the array uses.

Return Value

Return ValueDescription
array|null|falseAn array for the next row, null when there are no more rows, or false on failure.

PHP Examples

Example 1 — Fetch One Row as an Associative Array

Run a SELECT, then read the first row with MYSQLI_ASSOC. The printed values are a fixed sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products ORDER BY id LIMIT 1");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

echo $row["id"], " ", $row["name"], PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() opens a demo connection to the shop database.
  2. mysqli_query() runs a SELECT that returns one product row.
  3. mysqli_fetch_array() with MYSQLI_ASSOC returns that row keyed by column name.
  4. We print id and name, then free the result and close the connection.

Example 2 — Compare BOTH, ASSOC, and NUM

Fetch the same style of row three ways and print the keys each mode creates. Sample keys are fixed for the tutorial.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products LIMIT 1");

$both = mysqli_fetch_array($result, MYSQLI_BOTH);
mysqli_data_seek($result, 0);
$assoc = mysqli_fetch_array($result, MYSQLI_ASSOC);
mysqli_data_seek($result, 0);
$num = mysqli_fetch_array($result, MYSQLI_NUM);

echo "BOTH keys: ", implode(", ", array_keys($both)), PHP_EOL;
echo "ASSOC keys: ", implode(", ", array_keys($assoc)), PHP_EOL;
echo "NUM keys: ", implode(", ", array_keys($num)), PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. One result set is reused with mysqli_data_seek() so each mode sees the same first row.
  2. MYSQLI_BOTH includes both positions and names.
  3. MYSQLI_ASSOC keeps only column names.
  4. MYSQLI_NUM keeps only 0-based positions.

Example 3 — Loop Until There Are No More Rows

Keep calling mysqli_fetch_array() until it returns null, printing each product name from a sample result.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products ORDER BY id LIMIT 3");

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo $row["name"], PHP_EOL;
}

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The query asks for three product names.
  2. The while condition fetches the next associative row.
  3. When mysqli_fetch_array() returns null, the loop stops.
  4. Each sample name is printed on its own line.

Frequently Asked Questions

It returns the next row from a mysqli_result as an array. The default mode MYSQLI_BOTH gives both numeric and associative keys.
mysqli_fetch_assoc() always uses column-name keys. mysqli_fetch_array() can use ASSOC, NUM, or BOTH depending on the $mode argument.
When every row has already been read. Use that null result to stop a while loop.
Call $result->fetch_array($mode) on a mysqli_result object. It matches the procedural function.
Only if you need both key styles. Otherwise prefer MYSQLI_ASSOC or MYSQLI_NUM so each value is stored once.

Next: mysqli_fetch_assoc()

mysqli_fetch_assoc() reads the next row from a mysqli_result and returns it as an associat.

mysqli_fetch_assoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful