SQL Query Returned Null, Empty for JSON

Asked

Viewed 435 times

3

I’m in trouble!! I have two tables 'user'e 'post' when I use phpmyadmin to make the following query it returns the values as I hope.

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

consulta

but when I do php does not return me anything and worse even if you want an error. follows the code used.

// Instancia o objeto PDO
$pdo = new PDO("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);

Obs: strange is when I take the title and the description it returns me the name, photo_profile and the timestamp

{
    "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"
    },
  • It returns nothing from database to application or returns normal and when it creates json it creates json blank?

  • when I try to return normal (only printing the values )without using the json it works, but the strange thing is when I try to mount the json and put information from the table 'post' it comes empty

  • It can be some value inside some string breaking the json, trying to get the value of a line (Where user.id = X) and trying to mount Json.

  • continues the same empty way, I made a test by printing "print_r($Results);" and it returned the values but in json_encode does not come empty

  • Try to put echo json_encode(utf8_encode($Results));

  • Ricardo ele da erro "Warning: utf8_encode() expects Parameter 1 to be string, array Given in"

  • Then put it to run for each string that will enter the array. If it doesn’t then I run out of ideas.

  • Ricardo did the following put the utf8_encode($line['titulp]) in each line of the query and worked, valeeeeu

  • I put the result of the conversation in the answer to make it easier for other people to solve the problem. If you want to mark as an answer.

Show 4 more comments

1 answer

2


Putting the result of the comments in the reply:

For Json to work in PHP you need Encode utf8.

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

  • Json works with or without use of utf8_encode(). The problem in your case is the lack of environment configuration and therefore needed to do this "gambiarra" with utf8_encode()..

Browser other questions tagged

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