Home > Backend Development > PHP Tutorial > Why Am I Getting 'Object of class mysqli_result could not be converted to string' When Using mysqli_query()?

Why Am I Getting 'Object of class mysqli_result could not be converted to string' When Using mysqli_query()?

Patricia Arquette
Release: 2024-12-31 04:43:08
Original
791 people have browsed it

Why Am I Getting

Error: Object of Class mysqli_result Could Not Be Converted to String

When executing a MySQL query using the mysqli_query() function, you may encounter the following error: "Object of class mysqli_result could not be converted to string". This error occurs due to a misunderstanding of the output type returned by the function.

Cause:

The mysqli_query() function does not return a string; instead, it returns an object resource representing the result of the query. Using this object as a string, as attempted in the provided code, results in the error.

Solution:

To resolve this error, you need to correctly handle the object resource returned by mysqli_query(). You can do this by iterating over the result rows and accessing their values:

$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");

while ($row = $result->fetch_assoc()) {
    echo $row['classtype'] . "<br>";
}
Copy after login

In this corrected code, we use a while loop to iterate over each row in the result object, extracting the classtype column value and displaying it. This approach ensures that the result is displayed as a string.

The above is the detailed content of Why Am I Getting 'Object of class mysqli_result could not be converted to string' When Using mysqli_query()?. 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