How to fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP?

Spread the love

Sometimes, we want to fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP.

In this article, we’ll look at how to fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP.

How to fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP?

To fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP, we should make sure the connection object is defined.

For instance, we write

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

function getPosts()
{
    global $con;
    $query = mysqli_query($con, "SELECT * FROM Blog");
    while ($row = mysqli_fetch_array($query)) {
        //...
    }
}

to call mysql_connect with the database server URL and the credentials.

Then in the getPosts function, we make $con available with global.

This way, $con is referencing the $con variable at the top.

Conclusion

To fix Warning: mysqli_query() expects parameter 1 to be mysqli, null given in with PHP, we should make sure the connection object is defined.

Leave a Reply

Your email address will not be published.