Home > Backend Development > PHP Tutorial > Why Am I Getting 'Object of class mysqli_result could not be converted to string' When Querying MySQL?

Why Am I Getting 'Object of class mysqli_result could not be converted to string' When Querying MySQL?

Patricia Arquette
Release: 2024-12-18 15:16:11
Original
138 people have browsed it

Why Am I Getting

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

Issue:

When attempting to access data from a MySQL query using the mysqli_query() method, you may encounter the error, "Object of class mysqli_result could not be converted to string."

Reason:

This error occurs because the mysqli_query() method returns an object resource representing the result of the query, not a string. To access the actual data, you must iterate through the result object and extract the records.

Solution:

To fix this issue, you can use a loop such as the fetch_assoc() method to iterate through the result object and extract the rows of data. For example:

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

echo "my result ";

while ($row = $result->fetch_assoc()) {
    echo "<a href='data/" . $row['classtype'] . ".php'>" . "My account" . "</a><br>";
}
Copy after login

This revised code will correctly iterate through the result object and display the data for each row.

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