Content · Programming · PHP

PHP mysqli_result::fetch_array() Method: Usage and Examples

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

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to fetch the next MySQL result row using the object-oriented MySQLi API. This comes up when preparing data, displaying results, or integrating information with other services.

The mysqli_result::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_result::fetch_array() in PHP?

Fetch the next MySQL result row using the object-oriented MySQLi API.

Its syntax is:

mysqli_result::fetch_array(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: This method fetches one row per call and returns null at the end of the result set. Database access is required.

Parameters:

  • $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_result::fetch_array().

<?php

$database = new mysqli("localhost", "app", "secret", "demo"); $result = $database->query("SELECT id, title FROM posts LIMIT 2"); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { echo $row["title"] . "\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: fetch the next MySQL result row using the object-oriented MySQLi API.

Example 2: Understanding Another Case

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

<?php

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

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. This method fetches one row per call and returns null at the end of the result set. Database access is required.

Practical Example: A Real Application Scenario

Here is a practical use of mysqli_result::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 title FROM posts WHERE id = ?"); $postId = 8; $statement->bind_param("i", $postId); $statement->execute(); foreach ($statement->get_result() as $row) { echo htmlspecialchars($row["title"], 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. This method fetches one row per call and returns null at the end of the result set. Database access is required.

Important Behavior and Edge Cases

This method fetches one row per call and returns null at the end of the result set. Database access is required.

  • 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_result::fetch_array()Fetch the next MySQL result row using the object-oriented MySQLi API.
Related functionsmysqli_fetch_array() is the procedural counterpart; fetch_assoc() avoids numeric duplicates.

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 rowsThis method fetches one row per call and returns null at the end of the result set. Database access is required.

Key Takeaways

  • mysqli_result::fetch_array() lets you fetch the next MySQL result row using the object-oriented MySQLi API.
  • This method fetches one row per call and returns null at the end of the result set. Database access is required.
  • mysqli_fetch_array() is the procedural counterpart; fetch_assoc() avoids numeric duplicates.
  • Check input, return values, and environmental requirements when adapting the examples.

mysqli_result::fetch_array() provides a focused PHP operation for fetch the next MySQL result row using the object-oriented MySQLi API. 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.