Warning: mysqli_query() expects parameter 1 to be mysqli, null given in

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in

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

Stefan Bogdanescu

Founder & Senior Architect

The Silent Null: Why Your MySQLi Connection Variable Is Empty

The error message Warning: mysqli_query() expects parameter 1 to be mysqli, null given is deceptively simple. It tells you that you are passing null into a function expecting an active connection object. However, it rarely explains why that variable holds a null value when your code clearly shows a successful mysqli_connect() call. The root cause almost always lies in scope resolution and variable visibility within PHP functions.

In the provided snippet, the global $con variable is established outside of any function. Yet, inside getPosts(), the code attempts to use $con directly. While this works if $con is defined globally, relying on implicit global variables is brittle and often leads to exactly this kind of runtime error if the variable isn't accessible in that specific context or has been reset unexpectedly.

To resolve this, you must explicitly pass the connection resource to your functions. This shifts the responsibility from a global state to explicit argument passing. Here is the corrected implementation:

$con = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxx");

// Check connection immediately
if (mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

function getPosts($connection) {
    // Pass the connection object as an argument
    $query = mysqli_query($connection, "SELECT * FROM Blog");
    
    if (!$query) {
        // It is best practice to check query errors before iterating
        die("Query failed: " . mysqli_error($connection));
    }

    while($row = mysqli_fetch_array($query)) {
        echo "<div class=\"blogsnippet\">";
        echo "<h4>" . $row['Title'] . "</h4>" . $row['SubHeading'];
        echo "</div>";
    }
}

// Call the function, passing the connection explicitly
getPosts($con);

By passing $con as an argument to getPosts(), you ensure that the function receives exactly what it needs without relying on the global namespace. This pattern is not just about fixing a warning; it is about enforcing encapsulation. When a function depends on external global state, it becomes harder to test in isolation and more prone to "spooky action at a distance" bugs where a change in one part of the script breaks logic elsewhere.

There is a secondary pitfall worth addressing immediately: error checking inside the while loop. In your original code, you call mysqli_fetch_array() without verifying if $query actually returned a result set. If the SQL query fails silently or returns empty due to permissions issues, the loop might behave unexpectedly depending on the PHP version and specific MySQL configuration. Adding an explicit check after mysqli_query() ensures you catch database errors early rather than letting them manifest as cryptic null pointer warnings later.

Modern PHP development strongly favors passing resources explicitly over using global variables. This approach scales much better as your codebase grows. If you are building a larger application, consider wrapping this logic in a class or using dependency injection to manage the connection lifecycle. For instance, creating a simple Database class that holds the connection and exposes methods for fetching data keeps your core logic clean and your dependencies clear.

If you prefer managing database interactions within a framework ecosystem rather than raw PHP, platforms like Laravel handle connection management through service providers and repositories. You might explore resources such as https://laravelcompany.com to see how modern frameworks abstract these low-level details, allowing you to focus on business logic instead of manual resource passing. However, understanding the underlying mechanics of mysqli_query remains essential for debugging issues when stepping outside the framework's default configurations.

Our network