mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding the mysqli_fetch_array() Boolean Error

Encountering the error mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given is a classic symptom of a silent query failure. When this message appears on localhost but not on production hosting, the root cause is almost invariably how your environment handles connection parameters or string encoding, rather than a fundamental logic flaw in your PHP script.

The error occurs because mysqli_query() returned a boolean false instead of a result object. This happens when the SQL statement fails to execute. While it might seem like a simple syntax error, the fact that the code works on hosting but fails locally points to specific environmental differences. The most common culprit in this scenario is the default character set.

Your PHP script connects to MySQL using mysqli, but unless explicitly configured otherwise, the connection defaults to utf8_general_ci. Facebook's API and many modern applications expect UTF-8 encoding. If your database or the connection string on localhost is not set to a compatible charset (like utf8mb4), special characters in names or emails can cause the query to fail silently. When the query fails, mysqli_query() returns false. Passing this boolean value directly into mysqli_fetch_array() triggers the type mismatch error you are seeing.

Securing the Connection and Query

The provided code snippet contains several critical security vulnerabilities that must be addressed alongside debugging the immediate error. The current implementation relies on string concatenation to build SQL queries, which makes the application highly susceptible to SQL injection attacks.

// VULNERABLE: Direct interpolation allows injection
$check1_task = "SELECT * FROM `users` WHERE `fb_id` = " . $fb_id;
$add = "INSERT INTO users (fb_id, full_name) VALUES ('$fb_id', '$full_name')";

An attacker could manipulate the $user_profile data to inject malicious SQL commands. To fix this, you must use prepared statements. This approach separates the SQL logic from the data, ensuring that user inputs are always treated as strings rather than executable code.

Here is how the corrected logic should look using mysqli_stmt:

$stmt = $con->prepare("SELECT * FROM `users` WHERE `fb_id` = ?");
$stmt->bind_param("s", $fb_id); // 's' indicates a string type for the parameter
$stmt->execute();
$result = $stmt->get_result(); // get_result() returns false on failure, preserving context

if ($result->num_rows > 0) {
    $user = $result->fetch_assoc();
    // User exists, load canvas application
} else {
    // Prepare insert statement separately to avoid reusing the SELECT cursor logic
    $insertStmt = $con->prepare("INSERT INTO users (fb_id, full_name, first_name, last_name, email) VALUES (?, ?, ?, ?)");
    $insertStmt->bind_param("ssss", $fb_id, $full_name, $first_name, $last_name);
    $insertStmt->execute();
}

Addressing the Localhost vs. Hosting Discrepancy

Why does this fail locally? On your hosting server, it is likely that the database connection string or the server configuration defaults to a compatible charset automatically. However, on localhost, your config.php or connection script might be missing an explicit init_command or character set setting.

To resolve the encoding issue specifically for local development, you should append the following line immediately after establishing your MySQL connection:

$con->set_charset('utf8mb4');

This ensures that any special characters in Facebook user profiles (such as accents in names) are handled correctly, preventing the query from returning false.

Optimizing Logic Flow

Additionally, the original code attempts to double-check the existence of the user immediately after an insert operation using the result of a previous query. This is logically flawed because once a row is inserted, the initial SELECT result set becomes invalid or irrelevant for that specific transaction context. The corrected approach above handles the check-first strategy cleanly without relying on stale variables.

By switching to prepared statements and explicitly setting the character set, you eliminate both the security risks and the mysqli_fetch_array() type errors. If you are building a larger application structure, consider adopting Laravel, which abstracts these connection details and provides robust query builders that handle encoding and binding automatically. You can find more about secure database practices in their documentation at https://laravelcompany.com.

Our network