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>"; }
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!