Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

mysqli_fetch_array() says the argument must be a mysqli_result, but got something else

Errormysqli_fetch_array(): Argument #1 must be of type mysqli_result

Short answer

mysqli_fetch_array() only accepts a mysqli_result object. You passed it something else, almost always false from a query that failed, or true from a statement that is not a SELECT. Look at what mysqli_query() returned before you fetch, and turn on mysqli exceptions so the real SQL error shows up first.

  • Applies to PHP 8.3, PHP 8.4, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump what the query call returned, and the error text mysqli kept. If the dump shows bool(false), the query failed and the error line says why.

    <?php
    $result = mysqli_query($link, $sql);
    var_dump($result);
    echo mysqli_error($link);
  2. 2Match what you see, then jump to its fix

    1. 1var_dump($result) prints bool(false) and mysqli_error() has a message such as an unknown column or tableThe query failed and returned falseFix
    2. 2var_dump($result) prints bool(true) right after an INSERT, UPDATE, DELETE or CREATEThe statement is not a SELECT, so mysqli_query() returned trueFix
    3. 3var_dump() of the variable shows a mysqli_stmt object, not a mysqli_resultA prepared statement or another object was passed instead of the resultFix

What does the error look like?

  • mysqli_fetch_array(): Argument #1 must be of type mysqli_result
  • mysqli_sql_exception (PHP 8.1 and later, when the query itself failed)

Cause 1 of 3

The query failed and returned false

1Check

The manual says mysqli_query() returns false on failure. The fetch call then receives false instead of a result object. Print the SQL string and run it by hand in the mysql client.

<?php
echo $sql, PHP_EOL;
echo mysqli_error($link), PHP_EOL;

2Fix

Fix the SQL the error points at. Then stop trusting the return value blindly: either test for false, or switch on strict reporting so a failed query throws mysqli_sql_exception at the line that caused it. From PHP 8.1 the default mode already throws, so there you see the exception first.

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $result = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($result)) {
        // use $row
    }
} catch (mysqli_sql_exception $e) {
    error_log($e->getMessage());
}

3Verify

Run the page again. A broken query now logs its own message instead of a type error, and a good query returns rows.

php -d display_errors=1 page.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The statement is not a SELECT, so mysqli_query() returned true

1Check

Only queries that produce a result set (SELECT, SHOW, DESCRIBE, EXPLAIN) return a mysqli_result. Any other successful statement returns true. Check which statement is stored in $sql at that point, especially if one variable is reused for a write and then a read.

<?php
var_dump($result instanceof mysqli_result);

2Fix

Fetch only from a SELECT. After a write, read mysqli_affected_rows() instead, and run a separate SELECT when you need rows back.

<?php
$ok = mysqli_query($link, "UPDATE users SET active = 1 WHERE id = 7");
echo mysqli_affected_rows($link);

$result = mysqli_query($link, "SELECT id, email FROM users WHERE id = 7");
$row = mysqli_fetch_array($result);

3Verify

The fetch line now receives an object, so the type error is gone.

php page.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A prepared statement or another object was passed instead of the result

1Check

The manual lists what $result may be: the value from mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). A statement object from mysqli_prepare() is not one of them.

grep -n "mysqli_prepare\|mysqli_fetch_array" page.php

2Fix

Call mysqli_stmt_get_result() on the executed statement and pass that to the fetch function.

<?php
$stmt = mysqli_prepare($link, "SELECT id, email FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result);

3Verify

Run the page; the fetch returns a row array, null when there are no more rows, or false on failure.

php page.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Call mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) once, right after you load the database file, on every PHP version.
  • Never pass the return value of mysqli_query() to a fetch function without knowing the statement was a SELECT.
  • Use one variable per query so a write cannot overwrite the SELECT you meant to read.
  • Log the exception message on the server and show visitors a plain error page.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.