Home > Article > Backend Development > Why Does 'mysqli_query() expects parameter 1 to be mysqli, object given' Error Occur?
"mysqli_query() expects parameter 1 to be mysqli, object given" Error in mysqli Query
This error occurs when using the mysqli_query() function to execute MySQL queries against an object that is not a valid mysqli object. Let's break down the issue and provide a solution.
The mysqli_query() function takes two required parameters: the mysqli object and the query string. In your code, the first parameter is $connection, which is an instance of the createCon class. The createCon class is not a valid mysqli object, which is why you encounter the error.
To fix this issue, you need to pass the myconn property of the $connection object as the first parameter to mysqli_query(). The myconn property is the mysqli object that represents the database connection. Here's the corrected code:
$result = mysqli_query($connection->myconn, $query);
With this change, you will be passing the correct mysqli object, and the error should be resolved. Remember, it is crucial to use the correct object type when invoking the mysqli_query() function to avoid such errors.
The above is the detailed content of Why Does 'mysqli_query() expects parameter 1 to be mysqli, object given' Error Occur?. For more information, please follow other related articles on the PHP Chinese website!