Receive json mysql data

Asked

Viewed 236 times

1

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

Here I call ->

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

Here I display ->

echo $nam = json_encode($clientes);

Upshot:

[
  {
    "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 makes the following mistake

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

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

I tried to put "true" after Code but it didn’t 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;
}
}

3 answers

3


this code won’t even work, because $dados is not an object, $jsonObj is what it is. Do it as soon as it works:

$jsonObj = json_decode($nam);

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

1

Thank you very much, thanks to the help I managed to solve in the following way.

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

0

Try it this way

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.