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