PHP mysqli_fetch_assoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_assoc() reads the next row from a mysqli_result and returns it as an associative array whose keys are the column names.

It is the same as mysqli_fetch_array($result, MYSQLI_ASSOC). When there are no more rows it returns null. The object style is $result->fetch_assoc().

Syntax

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

PHP
$row = mysqli_fetch_assoc($result);
CallWhat it does
mysqli_fetch_assoc($result)Returns the next row keyed by column name, or null at the end.
while ($row = mysqli_fetch_assoc($result)) { … }Loops through every remaining row until null.
$row["email"]Reads one column from the associative array after a successful fetch.

mysqli_fetch_assoc() is the most common way to read SELECT rows in mysqli. Column names become array keys, so $row["id"] reads the id column. Duplicate column names without aliases can overwrite each other.

Library declarations
PHP
mysqli_fetch_assoc(mysqli_result $result): array|null|false

Parameters

ParameterDescription
resultRequired. A mysqli_result object that still has unread rows.

Return Value

Return ValueDescription
array|null|falseAn associative array for the next row, null when finished, or false on failure.

PHP Examples

Example 1 — Read One Associative Row

Fetch the first user row and print two columns. Output is a fixed demo sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, email FROM users WHERE id = 1");
$row = mysqli_fetch_assoc($result);

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

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

How It Works

  1. A connection is opened to the demo shop database.
  2. The SELECT asks for one user by id.
  3. mysqli_fetch_assoc() returns that row as an associative array.
  4. We print id and email from the sample row.

Example 2 — Handle an Empty Result

When a query matches no rows, the first fetch returns null. Check before using the array.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, email FROM users WHERE id = 9999");
$row = mysqli_fetch_assoc($result);

if ($row === null) {
    echo "No user found.", PHP_EOL;
} else {
    echo $row["email"], PHP_EOL;
}

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

How It Works

  1. The WHERE clause looks for an id that does not exist in the sample data.
  2. mysqli_fetch_assoc() returns null because there is no next row.
  3. The if check prints a clear message instead of reading array keys.
  4. Always test for null before using $row.

Example 3 — List Rows in a While Loop

Walk every returned row and print each email address from a fixed three-row sample.

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

while ($row = mysqli_fetch_assoc($result)) {
    echo $row["email"], PHP_EOL;
}

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

How It Works

  1. The query returns up to three user emails.
  2. Each loop iteration fetches the next associative row.
  3. null ends the loop when the result is exhausted.
  4. Three sample addresses are printed.

Frequently Asked Questions

It returns the next row of a result set as an associative array keyed by column name, or null when there are no more rows.
mysqli_fetch_assoc() uses column names as keys. mysqli_fetch_row() uses numeric indexes starting at 0.
Use $result->fetch_assoc() on a mysqli_result object. Behavior matches the procedural call.
Yes. Each call moves to the next row. After the last row, further calls return null unless you seek back with mysqli_data_seek().
Without aliases, later columns can overwrite earlier ones in the associative array. Give columns unique aliases in the SELECT list.

Next: mysqli_fetch_column()

mysqli_fetch_column() reads the next row from a mysqli_result and returns a single column .

mysqli_fetch_column() 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