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
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?
– Ricardo
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
– Alessandro Barreto
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.
– Ricardo
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
– Alessandro Barreto
Try to put echo json_encode(utf8_encode($Results));
– Ricardo
Ricardo ele da erro "Warning: utf8_encode() expects Parameter 1 to be string, array Given in"
– Alessandro Barreto
Then put it to run for each string that will enter the array. If it doesn’t then I run out of ideas.
– Ricardo
Ricardo did the following put the utf8_encode($line['titulp]) in each line of the query and worked, valeeeeu
– Alessandro Barreto
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.
– Ricardo