Home >Backend Development >PHP Tutorial >How to Fix the 'Object of class mysqli_result could not be converted to string' Error in PHP?

How to Fix the 'Object of class mysqli_result could not be converted to string' Error in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-18 09:17:11727browse

How to Fix the

Troubleshooting "Object of class mysqli_result could not be converted to string" in PHP

The error "Object of class mysqli_result could not be converted to string" occurs when trying to use a mysqli_result object as a string.

Problem:

You have encountered the error in your code, particularly in the following line:

echo "my result <a href='data/$result.php'>My account</a>";

In this line, you are attempting to use the $result variable, which is an object of class mysqli_result, as a string.

Solution:

The issue is that the mysqli_query() method returns an object resource to the $result variable, not a string. To access the result of the query, you need to loop through the rows of the result set and retrieve the values for each row.

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

By using the fetch_assoc() method, you can iterate through the result set and access the value of the classtype column for each row.

The above is the detailed content of How to Fix the 'Object of class mysqli_result could not be converted to string' Error in PHP?. 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