SQL Query Returned Null, Empty for JSON

3

I'm in trouble !! I have two tables 'user' and 'post' when I use phpmyadmin to do the following query it returns me the values as I expect.

SELECT usuario.nome, usuario.foto_profile, post.titulo, post.descricao, post.local, post.latitude, post.longitude, post.data
FROM usuario
INNER JOIN post ON usuario.id = post.id_usuario

ButwhenIgowithphp,itdoesnotreturnanythingtomeandit'sevenworseifIwantanerror.followthecodeused.

//InstanciaoobjetoPDO$pdo=newPDO("mysql:host=$servername;dbname=$database", $username, $password);
// define para que o PDO lance exceções caso ocorra erros
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


// executa a instrução SQL
$consulta = $pdo->query("SELECT usuario.id,usuario.nome, usuario.foto_profile, post.titulo, post.descricao, post.local, post.latitude, post.longitude, post.data
FROM usuario
INNER JOIN post ON usuario.id = post.id_usuario");

$results = array();
while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
    // aqui eu mostro os valores de minha consulta
  $results['feed'][] = array(
      'nome' => $linha['nome'],
      'foto_perfil' => $linha['foto_profile'],
      'timestamp' => $linha['data'],
      'titulo' => $linha['local'],
      'descricao' => $linha['descricao'],
   );
}


echo json_encode($results);

  • {
        "feed": [{
            "nome": "Alessandro Barreto",
            "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
            "timestamp": "1448069250858"
        }, {
            "nome": "Alessandro Barreto",
            "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
            "timestamp": "1448069704176"
        }, {
            "nome": "Alessandro Barreto",
            "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
            "timestamp": "1448069737468"
        },
    
        
  • asked by anonymous 29.11.2015 / 17:05

    1 answer

    2

    Putting in the response the result of the comments:

    For Json to work in PHP it is necessary to encode utf8.

    In the above case you just put utf8_encode (string), in the strings coming from the query at the time of mounting the array, so the whole array will be in the correct format and the conversion to Json will work.

        
    29.11.2015 / 17:38