QuizCure
Find Available Solution Here!

Object of class mysqli_result could not be converted to String

Deepak Nov 09, 2024

Basically, the error Object of class [Class name] could not be converted to string will raise whenever we try to use an object as a string.

For example, Suppose we have stdclass as below

$student = new stdClass(); 
$student->name = "John";
$student->id = 123;

Now we are going to treat the object $student as a string. Therefore let's say we print as below

echo "Student::". $student;

Result: Object of class stdClass could not be converted to a string

And the same issue occurred once we try to use mysqli_query returned result as a string rather than an object

Let's see the following code

$servername = "localhost"; // Specify your server Host/IP address here if not localhost 
$username = "username"; // Replace your Database username here
$password = "db_pwd"; // Replace your Database password here
$db= "demo"; //Replace your database name here

$mysqli = new mysqli($servername,$username,$password,$db);

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect Database: " . $mysqli -> connect_error;
  exit();
}

$resultSet = mysqli_query($mysqli, "SELECT * FROM tbl_employee");

echo "Employee:". $resultSet;

Result: PHP Catchable fatal error: Object of class mysqli_result could not be converted to string

Explanation:

  • Here function mysqli_query is used to perform a specified query to the database.
  • As per mysqli_query definition, it will return FALSE on failure
    and mysqli_result object on the success of SELECT Query.
  • In our case, it returns a result object and we have tried to print this result object therefore it complains.

Now let's understand how to fetch database records and use them as strings. Find following Mysqli functions demonstrating for extracting data purpose.

Find number count for records matched with fetched

$count = mysqli_num_rows($resultSet);

Mysqli_num_rows function requires input as mysqli_result object and returns the number of rows matched with the specified query

Fetch data row using mysqli_fetch_array function for the specified query

To fetch records we can use the following Mysqli functions

  • mysqli_fetch_array ($resultSet) : Combination of numeric & associative data key values
  • mysqli_fetch_array($resultSet, MYSQLI_ASSOC) : For Associative array
  • mysqli_fetch_assoc($resultSet) : For Associative array
  • mysqli_fetch_object($resultSet) : For row as object
$servername = "localhost"; // Specify your server Host/IP address here if not localhost 
$username = "uname"; // Replace your Database username here
$password = "pwd"; // Replace your Database password here
$db= "demo"; //Replace your database name here

$mysqli = new mysqli($servername,$username,$password,$db);

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect Database: " . $mysqli -> connect_error;
  exit();
}

$resultSet = mysqli_query($mysqli, "SELECT name, email FROM tbl_employee");

echo '<pre>';
if (mysqli_num_rows($resultSet) > 0) {
  while($row = mysqli_fetch_array($resultSet)){
            
            print_r($row);
      
  }
}
 
//Created one record (name: Test & email : testing@gmail.com) in table tbl_employee for demo purpose 
Result:
Array
(
    [0] => Test
    [name] => Test
    [1] => testing@gmail.com
    [email] => testing@gmail.com
)
 

Explanation:

  • mysqli_fetch_array is used to return table row data as specified SELECT columns.
  • It is required to pass the result set identifier as returned by mysqli_query.
  • Default mysqli_fetch_array returns array containing numeric key & table column name as a key.
  • Can use like $row = mysqli_fetch_array($resultSet, MYSQLI_ASSOC) by setting MYSQLI_ASSOC to get an associative array only.
  • Also we can use $row = mysqli_fetch_assoc($resultSet) instead of mysqli_fetch_array($resultSet, MYSQLI_ASSOC) for an associative array.

Was this post helpful?

Send Feedback

Practice Multiple-Choice Questions

  • ?
    What will be the outcome of following mysqli_query code?
    $mysqli = new mysqli($servername,$username,$password,$db);
    
    $resultSet = mysqli_query($mysqli, "SELECT name FROM tbl_employee where id=1");
    
    echo $resultSet;
    
    Options are:
    Try your hand at more Multiple-choice Exercises

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up