I keep having Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, bool given

I keep having Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, bool given

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

Stefan Bogdanescu

Founder & Senior Architect

I keep having Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, bool given

This error message is one of the most common stumbling blocks for developers transitioning from procedural PHP to object-oriented patterns or using newer PHP versions. The specific error Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, bool given occurs because you are passing a boolean value where an instance of the mysqli class is expected.

In your ServerForm.php, line 70 contains the culprit. You likely have code resembling this:

echo mysqli_error($db);
// or
echo mysqli_error($insertResult);

When you execute mysqli_query() directly, it returns a boolean value on failure and an object on success. In older PHP versions (5.6 and below), the return type wasn't strictly enforced in this manner, but in PHP 7+ and with the new MySQLi driver extensions, mysqli_error() strictly requires a valid resource or object representing the connection. If you pass a boolean false (indicating a failed query) into mysqli_error(), it throws a TypeError because there is no database context associated with that boolean to retrieve an error message from.

The Connection Object vs. Query Results

The primary fix involves understanding what your variables actually hold at runtime. When you run:

$result2 = mysqli_query($db, $checkexistemail);

If $checkexistemail fails (e.g., syntax error, connection issue), $result2 becomes false. Your current code attempts to check the result with if (!($result2)), which is correct for logic flow, but then you might be trying to call mysqli_error($result2). Since $result2 is now a boolean, the function throws the TypeError.

To retrieve the specific error message from a failed query or connection, you must pass the connection object ($db) to mysqli_error(), not the result variable. The function looks up the last error that occurred on that specific connection instance.

Here is the corrected approach for handling errors after a failed query:

if (!$result2) {
    // Pass $db, not $result2
    echo mysqli_error($db); 
}

Security Vulnerabilities in Your Current Implementation

While fixing the TypeError, it is critical to address the severe security flaws present in your ServerForm.php logic. You are constructing SQL queries using direct string concatenation:

$query="INSERT INTO 'Users'('Fname', 'Lname', 'Email', 'Password', 'Pnumber', 'Address') 
      VALUES('$Fname', '$Lname', '$email','$psw', '$t', '$add)";"

This is a textbook example of SQL Injection. If a user inputs '; DROP TABLE Users; -- into the email field, their input becomes part of the SQL command before it is executed. This allows attackers to bypass authentication, delete data, or exfiltrate sensitive information.

You must stop using string concatenation for database queries. Instead, use prepared statements. Prepared statements separate the SQL logic from the data, ensuring that user inputs are treated strictly as data and never as executable code.

Here is how you should refactor your insertion logic using prepared statements:

$stmt = $db->prepare("INSERT INTO Users (Fname, Lname, Email, Password, Pnumber, Address) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $Fname, $Lname, $email, $psw, $t, $add);

if ($stmt->execute()) {
    $insertSuccess = true;
} else {
    // Safely get the error for the prepared statement or connection
    echo "Insert failed: " . $stmt->error; 
}

$stmt->close();

Handling Multiple Validation Errors

Your second issue regarding displaying duplicate emails, phone numbers, and password mismatches stems from how you are managing the execution flow. Currently, your script checks for existence and then immediately attempts insertion. If multiple conditions fail (e.g., email exists AND passwords don't match), your script prints errors sequentially but does not prevent the form from resubmitting or entering an inconsistent state.

A robust registration flow should validate all inputs first before attempting any database writes. Only after confirming that:

  1. The email is unique.
  2. The phone number is unique.
  3. The passwords match.

Should you proceed to the INSERT statement. If validation fails, set your error flags (like $duplicateEmail) and re-render the form with the error messages displayed near the corresponding input fields. Do not attempt to insert data if a validation check has failed; doing so creates race conditions where one user might accidentally create an account for another by submitting while a duplicate email is being validated in the background.

Conclusion

To resolve the Uncaught TypeError, ensure that mysqli_error() always receives the $db connection object, not a boolean result variable. Simultaneously, migrate your codebase to prepared statements to eliminate SQL injection risks. By separating validation logic from data persistence and handling connection objects correctly, you will stabilize your application and adhere to modern PHP security standards.

Our network