Return Json using Mysql [closed]

1

I have 3 tables I do the inner join in them, but I need to return a json, I just got a simple select return:

My Sql clause:

SELECT publicacao.codPublicacao as codpublicacao,
       usuario.nome as nomeusuario,
       publicacao.categoria as categoria,
       publicacao.endereco as endereco,
       publicacao.tipo as tipo,
       publicacao.descricao as descricao,
       cidade.nome as nomecidade, publicacao.contato as contato
FROM 'publicacao'
inner join usuario on publicacao.codUsuario = usuario.codUsuario
inner join cidade on publicacao.codcidade = cidade.codcidade

Php:

  $sql = "SELECT publicacao.codPublicacao as codpublicacao,usuario.nome as 
            nomeusuario, publicacao.categoria as categoria,
            publicacao.endereco as endereco, publicacao.tipo as tipo, 
            publicacao.descricao as descricao, cidade.nome as nomecidade,
            publicacao.contato as contato FROM 'publicacao' inner join usuario on 
        publicacao.codUsuario = usuario.codUsuario inner join cidade on 
    publicacao.codcidade = cidade.codcidade"; 
                        $result = $conn->query($sql);
                    $encode = array();

                     while ($row = $result->fetch_assoc()) {
                       $encode[] = json_encode($row);

                    }
echo json_encode($encode);

?>
    
asked by anonymous 21.06.2016 / 15:53

1 answer

0

Why do not you use it this way?

while ($row = $result->fetchAll()) {
    $encode[] = $row;
}
echo json_encode($encode);

Even the previous mode was returning json correctly, but that's enough.

If it does not work, look at the error log, maybe it's popping the php memory.

    
21.06.2016 / 17:59