Home > Backend Development > PHP Tutorial > Why does `mysqli_query()` return a 'Warning: mysqli_query() expects parameter 1 to be mysqli, null given' error?

Why does `mysqli_query()` return a 'Warning: mysqli_query() expects parameter 1 to be mysqli, null given' error?

Mary-Kate Olsen
Release: 2024-12-05 00:53:14
Original
308 people have browsed it

Why does `mysqli_query()` return a

Warning: Understanding mysqli_query() Parameter Error

When using mysqli_query() in PHP, it's crucial to ensure that the first parameter is a valid mysqli object representing a database connection. However, if you encounter an error stating "Warning: mysqli_query() expects parameter 1 to be mysqli, null given in," it indicates that the first parameter passed to the function is not a mysqli object.

Examining the Code

In your provided code, you have a function called getPosts(). This function attempts to query a database table named Blog. However, in the mysqli_query() call within getPosts(), the first parameter is not explicitly passed. As a result, PHP assumes the value of $con, which is declared outside the function, as the first parameter. But, $con is not in the scope of getPosts() and hence returns null.

Resolving the Error

To resolve this issue, you have two options:

  1. **Globalize the $con Variable:** You can make the $con variable global inside getPosts() using the global $con; declaration. This will tell PHP to look for $con outside the function, making it accessible within getPosts().
  2. **Pass the $con Variable as a Parameter:** A better approach is to pass the $con object as a parameter to the getPosts() function. This ensures that getPosts() has direct access to the database connection, eliminating the need for global scope.

Example:

function getPosts(mysqli $con) {
    // ... same code as before
}

// Connect to the database
$con = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxx");
// Call getPosts() with the connection as a parameter
getPosts($con);
Copy after login

Additional Recommendations

It's also advisable to handle any potential errors that may occur during database operations. You can enable error reporting explicitly as follows:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
Copy after login

This will throw exceptions if any errors occur, allowing you to take appropriate action in your code.

The above is the detailed content of Why does `mysqli_query()` return a 'Warning: mysqli_query() expects parameter 1 to be mysqli, null given' error?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template