Receive json mysql data

1

Hello, I have the following function to read DB data mysql.

Here I call - >

$clientes = DBRead('clientes', null, 'nome, telefone1');

Here I display - >

echo $nam = json_encode($clientes);

Result:

[
  {
    "nome":"Davi",
    "telefone1":"654654"
  },
  {
    "nome":"Davi",
    "telefone1":null
  },
  {
    "nome":"Davi",
    "telefone1":null
  }
]

Here I try to manipulate the results separately:

$jsonObj = json_decode($nam);

    $dados = $jsonObj->nome;

    foreach ( $dados as $e ) { echo "nome: $e->nome "; }

but gives the following error

  

Notice: Trying to get property of non-object in ...

     

Warning: Invalid argument supplied for foreach () in ...

I tried to put ", true" after the decode but it did not work.

Does anyone have an idea how I can handle this data?

Here the Search function in another file->

    function DBRead($table, $params = null, $fields = '*'){
    $table = $table;
    $params = ($params) ?" {$params}" : null;
$query = "SELECT {$fields} FROM {$table}{$params}";
$result = DBExecute($query);
if(!mysqli_num_rows($result))
    return false;
else{
    while ($res = mysqli_fetch_assoc($result)){
        $data[] = $res;
    }
    return $data;
}
}
    
asked by anonymous 30.03.2016 / 17:00

3 answers

2

This code will not work anyway, because $dados is not an object, $jsonObj is what it is. Do it right:

$jsonObj = json_decode($nam);

    foreach ( $jsonObj as $e ) { echo "nome: $e->nome "; }
    
30.03.2016 / 21:53
1

Thanks to everyone, thanks to the help I was able to solve as follows.

 $clientes = DBRead('clientes', null, '*');
  $nam = json_encode($clientes);
  $res = json_decode($nam, true);
  foreach ( $res as $e ) 
      { echo $e['nome']}
    
14.04.2016 / 12:13
0

Try this out

$jsonObj = json_decode($nam);
foreach ( $jsonObj as $e ) { 
echo "nome: $e->nome - telefone: $e->telefone1<br />"; 
}
    
30.03.2016 / 19:18