0

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

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Michael Shtefanitsa
  • 303
  • 1
  • 4
  • 14
  • 1
    Could you please add to your question an example of `$row['my_data']`? Thank you. – Syscall Mar 30 '18 at 09:14
  • 2
    json decode converts `json string` to `object` but you rather want a string, rather convert it to array and then use `implode` to merge or some loops, depends on `$row['my_data']` – frunkad Mar 30 '18 at 09:15

1 Answers1

1

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.

d.coder
  • 1,988
  • 16
  • 23