W3docs

fetch_assoc

In this article, we will focus on the mysqli_fetch_assoc() function in PHP, which is used to fetch a row from a MySQLi result set as an associative array. We

The mysqli_fetch_assoc() function fetches one row from a MySQLi result set and returns it as an associative array, where each key is a column name from your query. This page explains its syntax, return value, how it behaves inside a loop, the most common mistakes, and how it differs from related fetch functions.

What mysqli_fetch_assoc() does

When you run a SELECT query, MySQLi gives you back a result set — a pointer to the rows the database matched. mysqli_fetch_assoc() pulls the next row from that set and hands it to you as an array keyed by column name:

$row = mysqli_fetch_assoc($result);
echo $row['email']; // access a value by its column name

Each call advances an internal cursor, so calling the function repeatedly walks through the rows one at a time. When there are no rows left, it returns null.

Syntax

mysqli_fetch_assoc(mysqli_result $result): array|null|false
PartMeaning
$resultA result object returned by mysqli_query() (or mysqli_store_result() / mysqli_use_result()).
ReturnAn associative array for the fetched row, null when there are no more rows, or false on failure.

If two columns in your SELECT share the same name (for example a JOIN returning two id columns), only the last one survives in the array — use column aliases (SELECT a.id AS user_id) to keep both.

Fetching rows in a loop

You almost always call mysqli_fetch_assoc() inside a while loop. Because it returns null (falsy) after the last row, the loop ends on its own:

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "shop");

if (mysqli_connect_errno()) {
    die("Connection failed: " . mysqli_connect_error());
}

$result = mysqli_query($mysqli, "SELECT id, name, price FROM products");

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['id'] . ": " . $row['name'] . " ($" . $row['price'] . ")\n";
    }
    mysqli_free_result($result);
} else {
    echo "Query failed: " . mysqli_error($mysqli);
}

mysqli_close($mysqli);
?>

We check the connection with mysqli_connect_errno(), run the query, then loop. Each iteration assigns the next row to $row; when the set is exhausted the assignment yields null, the while condition becomes false, and the loop stops. mysqli_free_result() releases the memory held by the result set once we are done.

Seeing the array it returns

You do not need a database to understand the shape of a row. The snippet below builds an array identical to what a single mysqli_fetch_assoc() call would return and prints it, so you can see the column-name keys:

<?php
// This is the structure mysqli_fetch_assoc() hands back for one row.
$row = [
    "id"    => 1,
    "name"  => "Keyboard",
    "price" => 29.99,
];

echo "Product: " . $row["name"] . "\n";
echo "Price:   $" . $row["price"] . "\n";

print_r($row);
?>

Running it prints:

Product: Keyboard
Price:   $29.99
Array
(
    [id] => 1
    [name] => Keyboard
    [price] => 29.99
)

Common mistakes

  • Calling it on a failed query. mysqli_query() returns false on error, and passing false to mysqli_fetch_assoc() is invalid. Always check $result first, as in the loop above.
  • Forgetting it only returns one row. A single call gives you the next row, not the whole table. Loop to read them all.
  • Comparing with == instead of assignment. The loop uses $row = mysqli_fetch_assoc($result) (assignment). Writing == would never advance the cursor and could loop forever.
  • Treating it like numeric access. Use $row['name'], not $row[0]. For numeric keys use mysqli_fetch_row(); for both, use mysqli_fetch_array().

How it compares to other fetch functions

FunctionReturns each row as
mysqli_fetch_assoc()Associative array ($row['name'])
mysqli_fetch_row()Numeric array ($row[0])
mysqli_fetch_array()Both associative and numeric keys
mysqli_fetch_object()An object ($row->name)
mysqli_fetch_all()An array of all rows at once

Reach for mysqli_fetch_assoc() when you want readable, self-documenting code that refers to columns by name and you are processing rows one at a time.

Conclusion

mysqli_fetch_assoc() is the go-to function for reading a MySQLi result set as associative arrays keyed by column name. Combine it with a while loop and a quick check that the query succeeded, and you can iterate result sets safely. For background, see PHP MySQLi, associative arrays, and the while loop.

Practice

Practice
What does the fetch_assoc() function in PHP do?
What does the fetch_assoc() function in PHP do?
Was this page helpful?