Content · Programming · PHP

PHP mysqli_fetch_array() Function: Usage and Examples

Learn mysqli_fetch_array() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to retrieve the next MySQL result row as a numeric, associative or combined array. This comes up when preparing data, displaying results, or integrating information with other services.

The mysqli_fetch_array() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is mysqli_fetch_array() in PHP?

Retrieve the next MySQL result row as a numeric, associative or combined array.

Its syntax is:

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

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: Returns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.

Parameters:

  • $result — A named input or configuration argument; use the declared type and optional default shown above.
  • $mode — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of mysqli_fetch_array().

<?php

$database = new mysqli("localhost", "app", "secret", "demo"); $result = $database->query("SELECT id, name FROM members LIMIT 2"); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo $row["name"] . "\n"; }

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

The output demonstrates the primary operation: retrieve the next MySQL result row as a numeric, associative or combined array.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of mysqli_fetch_array().

<?php

$database = new mysqli("localhost", "app", "secret", "demo"); $result = $database->query("SELECT id, name FROM members LIMIT 1"); $row = mysqli_fetch_array($result, MYSQLI_NUM); print_r($row);

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

Compared with the first example, this one illustrates the same operation with a different input shape or option. Returns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.

Practical Example: A Real Application Scenario

Here is a practical use of mysqli_fetch_array() with realistic sample information. Adapt the values to match your application.

<?php

$database = new mysqli("localhost", "app", "secret", "demo"); $statement = $database->prepare("SELECT id, name FROM members WHERE id = ?"); $memberId = 5; $statement->bind_param("i", $memberId); $statement->execute(); $result = $statement->get_result(); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo htmlspecialchars($row["name"], ENT_QUOTES, "UTF-8"); }

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

This is a useful starting point, but production code should check errors and validate values before performing the operation. Returns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.

Important Behavior and Edge Cases

Returns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
mysqli_fetch_array()Retrieve the next MySQL result row as a numeric, associative or combined array.
Related functionsmysqli_fetch_assoc() returns only named keys; mysqli_fetch_row() returns numeric keys.

Common Beginner Mistakes

MistakeCorrect Approach
Building SQL by concatenating inputBind data values through prepared statements; do not interpolate user data into SQL.
Assuming a live database is availableConfigure the required driver, schema, and connection, then check query errors.
Confusing raw COPY data with fetched rowsReturns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.

Key Takeaways

  • mysqli_fetch_array() lets you retrieve the next MySQL result row as a numeric, associative or combined array.
  • Returns null when no rows remain; MYSQLI_BOTH produces duplicate named and numbered entries. Database and credentials are placeholders.
  • mysqli_fetch_assoc() returns only named keys; mysqli_fetch_row() returns numeric keys.
  • Check input, return values, and environmental requirements when adapting the examples.

mysqli_fetch_array() provides a focused PHP operation for retrieve the next MySQL result row as a numeric, associative or combined array. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.