Search code examples
phpmysqlarraysjsonjsondecoder

Catchable fatal error: in json_decode


I want to eliminate JSON view format and return data like a string with coma.

When i use json_decode($row['test_row']) it returns me

Catchable fatal error: Object of class stdClass could not be converted to string in C

<?php 
try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password );    
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = 'SELECT * FROM database.test;' ;
        $get_all_data = $conn->prepare($sql);
        $get_all_data -> execute(array($sql));
        $all_row = $get_all_data->fetch(PDO::FETCH_ASSOC); 
        $all = $all_row;
    }
   catch(PDOException $e)
         {
           echo $sql . "<br>" . $e->getMessage();
         }

here is table, table head:

    echo "<tbody>";
    echo "<table>";
          $conn = null;   
    while($row = $get_all_data->fetch(PDO::FETCH_ASSOC)) 
{   

echo "<tr>  
       <td>" . json_decode($row['my_data']) . "</td> 
      </tr>";  
} 
    echo "</tbody>";
    echo "</table>";  ?>

If I'll let $row['my_data'], it returns me data from database in JSON format


Solution

  • json_decode returns PHP object which then you are trying to print as a string using echo. This is throwing this error.

    You will have to make it a comma separated string by using implode or such function. It will then definitely work.

    As i don't know value of $row['my_data'] so can suggest you exact piece of code.