Home  >  Article  >  Backend Development  >  Why does mysqli_query() require a MySQLi object as its first parameter?

Why does mysqli_query() require a MySQLi object as its first parameter?

Barbara Streisand
Barbara StreisandOriginal
2024-11-23 05:47:14237browse

Why does mysqli_query() require a MySQLi object as its first parameter?

Error: mysqli_query() Expects First Parameter to Be MySQLi

Problem:

When using a class to connect to a MySQL database, an error is encountered:

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

Underlying Issue:

The issue arises when attempting to execute a query using mysqli_query(). The function expects the first parameter to be a MySQLi object, but the code is currently passing an instance of the class instead.

Solution:

To resolve the error, pass $connection->myconn instead of $connection as the first parameter to mysqli_query().

Here is the corrected code:

$result = mysqli_query($connection->myconn, $query);

Explanation:

The class property $myconn holds the MySQLi connection object. By passing $connection->myconn, we are correctly passing the required MySQLi object to mysqli_query().

The above is the detailed content of Why does mysqli_query() require a MySQLi object as its first parameter?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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