Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of class mysqli_result could not be converted to string

Tags:

php

mysqli

I am getting the error:

Object of class mysqli_result could not be converted to string

This is my code:

$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");  echo "my result <a href='data/$result.php'>My account</a>"; 
like image 948
Chinmay Chandak Avatar asked Feb 12 '14 08:02

Chinmay Chandak


People also ask

What is mysqli_result?

The mysqli_result class ¶Represents the result set obtained from a query against the database.

What is the return type of Mysqli_query?

Return Values ¶ For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN , mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true .

How do you submit a query to mysql using Mysqli?

Example. <? php //Creating a connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Executing the multi query $query = "SELECT * FROM players"; //Retrieving the records $res = mysqli_query($con, $query, MYSQLI_USE_RESULT); if ($res) { while ($row = mysqli_fetch_row($res)) { print("Name: ".


2 Answers

The mysqli_query() method returns an object resource to your $result variable, not a string.

You need to loop it up and then access the records. You just can't directly use it as your $result variable.

while ($row = $result->fetch_assoc()) {     echo $row['classtype']."<br>"; } 
like image 133
Shankar Narayana Damodaran Avatar answered Sep 30 '22 15:09

Shankar Narayana Damodaran


Before using the $result variable, you should use $row = mysqli_fetch_array($result) or mysqli_fetch_assoc() functions.

Like this:

$row = mysqli_fetch_array($result); 

and use the $row array as you need.

like image 42
Chandan Singh Gadhwal Avatar answered Sep 30 '22 14:09

Chandan Singh Gadhwal