Home > Backend Development > PHP Tutorial > Why does mysqli_query() expect a mysqli parameter, but I'm getting an object instead?

Why does mysqli_query() expect a mysqli parameter, but I'm getting an object instead?

Patricia Arquette
Release: 2024-11-15 01:35:02
Original
965 people have browsed it

Why does mysqli_query() expect a mysqli parameter, but I'm getting an object instead?

mysqli_query() Expects mysqli Parameter: Solving the Parameter Type Issue

When working with PHP and MySQL, you may encounter a common error related to mysqli_query() expecting a mysqli parameter instead of an object. This error usually occurs when you try to make a database query using an object reference instead of the actual mysqli connection resource.

Understanding the Error Message

The error message "mysqli_query() expects parameter 1 to be mysqli, object given" indicates that function expects the first parameter to be an instance of the mysqli class, but instead, it is receiving an object of a different type.

Resolving the Issue

To resolve this error, you need to pass the correct parameter type to mysqli_query(). In your provided code, the query should be executed using the following code:

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

In this line, $connection->myconn refers to the actual mysqli connection resource, which is an instance of the mysqli class. By passing this connection resource to mysqli_query(), you avoid the error and execute the query correctly.

Explanation

In your original code, where you wrote "$connection," and you were trying to pass the entire createCon object. By accessing $connection->myconn, you are targeting the mysqli connection resource within the createCon object, which is the correct parameter type for mysqli_query().

By making this simple change, you can resolve the error and successfully execute your database queries using mysqli_query().

The above is the detailed content of Why does mysqli_query() expect a mysqli parameter, but I'm getting an object instead?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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