Warning: mysqli_query() expects parameter 1 to be mysqli boolean given

Warning: mysqli_query() expects parameter 1 to be mysqli boolean given

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_query() expects parameter 1 to be mysqli boolean given

The error message Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given is one of those PHP warnings that immediately tells you exactly what is wrong with your logic flow, provided you know how the function signature works. This specific issue arises when a variable intended to hold a database connection object is actually holding a boolean value instead. In the scenario described, this happens because the code attempts to execute a query using $con, but $con has already been set to false by the previous line.

Let's look closely at the problematic block of code:

$con=mysqli_connect($mysqli_host,$mysql_username,$mysql_password,$site_db);
// Checks connection to twitch webpanel database and inserts registreation info
if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

The root cause lies in the conditional check if (mysqli_connect_errno());. The developer intended to verify if a connection error occurred. However, mysqli_connect_errno() returns an integer representing the last error number. It returns 0 on success and a non-zero value on failure. Crucially, this function does not return a boolean; it returns an integer.

In PHP 7.4 and later, loose type comparisons involving booleans became stricter in specific contexts, but even before that, passing the result of a numeric comparison check directly to mysqli_query creates a logical gap. If the connection fails, mysqli_connect_errno() returns a non-zero integer (e.g., 2002 for a connection refused). While PHP treats non-zero integers as truthy in an if statement, the logic inside that if block is flawed because there is no else block to handle the success case.

Here is what actually happens:

  1. Connection Fails: mysqli_connect() fails (e.g., MySQL service is off). The variable $con is set to false.
  2. Error Check: mysqli_connect_errno() returns a non-zero integer (say, 2).
  3. The Trap: The code enters the if block because the error number is truthy. It prints the error message.
  4. The Execution: The script continues to the next line: mysqli_query($con, ....
  5. The Crash: At this point, $con is still false. You are passing false as the first argument to mysqli_query(), which expects an instance of mysqli. This triggers the warning you see in the logs.

If the connection had succeeded, $con would hold a valid mysqli object, and the query would work. The error is strictly a control flow issue where the code assumes that if an error check prints something, execution stops there, but PHP does not automatically halt unless you use exit() or die().

Fixing the Logic Flow

To resolve this, you must explicitly handle both success and failure cases. You should verify the connection status immediately after calling mysqli_connect(). If the connection fails, you must stop the script before attempting any database operations.

Here is the corrected implementation:

$con = mysqli_connect($mysqli_host, $mysql_username, $mysql_password, $site_db);

// Check the connection result directly
if (!$con) {
    // Connection failed. Stop execution immediately.
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

// Only reach this point if the connection object is valid (mysqli instance)
mysqli_query($con, "INSERT INTO Users (Username, Password, Email, Subcription)
VALUES ('$info_name', '$info_pass', '$info_email', '$sub_type')");

Security Implications Beyond the Error

While fixing the boolean warning is critical for stability, the code snippet also contains severe security vulnerabilities that must be addressed. The query constructs SQL strings directly using variables: "$info_name", "$info_pass", etc. This opens the application to SQL Injection attacks. An attacker could manipulate the form inputs to alter the database structure or extract sensitive data.

You should never concatenate user input into SQL queries. Instead, use prepared statements with bound parameters. With the mysqli extension, this looks like:

$stmt = $con->prepare("INSERT INTO Users (Username, Password, Email, Subcription) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $info_name, $info_pass, $info_email, $sub_type);
$stmt->execute();

This approach ensures that the database treats the input as data, not executable code, regardless of what characters the user sends. Additionally, storing passwords in plain text (as implied by $info_pass) is a major risk; ensure you hash passwords using password_hash() before storing them.

Summary of Changes

  1. Check Connection Result: Use if (!$con) to verify the connection object exists before proceeding.
  2. Terminate on Failure: Call die() or exit() inside the failure block to prevent the script from running subsequent queries with a false value.
  3. Use Prepared Statements: Replace string concatenation in SQL queries with prepare() and bind_param() to mitigate injection risks.

By restructuring the connection logic and securing the query execution, you eliminate the warning and harden the application against common web vulnerabilities.

Our network