Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Misunderstanding the mysqli Object Model

The error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given is a classic symptom of passing a connection handle where a result set is expected. In your specific case, the issue lies in how you are invoking the query and subsequently fetching the data. You are treating the database connection as if it were the result of a query, which leads directly to the type mismatch error on line 32.

The root cause is the absence of an explicit mysqli_query call between your connection setup and your fetch loop. When you execute $connection = mysqli_connect(...), you establish a live link to the server. However, simply connecting does not execute any SQL statements. The variable $connection holds a resource representing the active session, not the output of a SELECT statement.

In the PHP Data Objects (PDO) extension or when using pure MySQLi with procedural calls, developers often confuse the connection resource with the result resource. With MySQLi, the architecture distinguishes strictly between the two:

  1. Connection (mysqli): Represents the session to the database server. Used for mysqli_connect, mysqli_close, and transaction control.
  2. Result (mysqli_result): Represents the output of a specific query like SELECT. Required for fetch_* methods.

Your code attempts to pass $connection (the first argument) and $sql (the second argument, which is technically ignored in this function signature) directly into mysqli_fetch_array. The function expects an instance of mysqli_result, but receives an instance of mysqli. This is why the warning triggers immediately.

To resolve this, you must execute the SQL string against the connection object to generate a result set before attempting to fetch rows. You need to insert a mysqli_query call between your connection logic and the loop. Here is the corrected procedural implementation:

$connection = mysqli_connect($host, $username, $password, $db_name);

// Verify the connection was successful
if ($connection === false) {
    die('Connection Error: ' . mysqli_connect_error());
}

$sql = 'SELECT * FROM provinsi';
$result = mysqli_query($connection, $sql);

// Check if the query returned any errors
if (!$result) {
    die('Query Error: ' . mysqli_error($connection));
}

// Now iterate over the result set
while ($r = mysqli_fetch_array($result)) { // Line 32 fixed: using $result instead of $connection
    $id = $r['id'];
}

mysqli_close($connection);

In this corrected version, $result captures the mysqli_result object returned by mysqli_query. Passing $result to mysqli_fetch_array() satisfies the type requirement. The second argument you are currently passing ($sql) is not used by mysqli_fetch_array; it expects an optional array of flags or a string indicating column mode, but never raw SQL code.

If you prefer the Object-Oriented API, the distinction remains similar but the syntax shifts slightly. You would instantiate a new mysqli object, call $connection->query($sql), and then pass that resulting object to $result->fetch_array().

Trade-offs and Best Practices

While procedural MySQLi works for simple scripts, it lacks the safety features inherent in the Object-Oriented approach or PDO. If you are building a larger application, consider migrating to PDO or sticking strictly to the OOP style of MySQLi (new mysqli()). These approaches enforce stricter type checking and make the separation between connection and result resources explicit in the code structure.

Additionally, always validate that mysqli_query returned true before attempting to fetch data. If a query fails due to syntax errors or missing tables, the result variable will be false. Attempting to fetch from a false result set will cause a fatal error rather than a warning later in your script.

For developers working within the Laravel ecosystem, handling raw database connections is generally discouraged unless you are implementing specific performance optimizations or legacy integrations. Laravel provides an elegant query builder and Eloquent ORM that abstract these low-level details away. If you need to execute this specific SELECT statement using Laravel's underlying connection, you should utilize the DB::select() method or a repository pattern, which returns an array of associative arrays directly, bypassing the need for manual resource management entirely.

Understanding the distinction between the connection handle and the result set is fundamental to writing robust database code in PHP. The error message is your best friend here; it explicitly tells you that the object type you are passing does not match the function's expectation, guiding you straight to the missing query call.

Our network