Problems with Json and php

Asked

Viewed 26 times

1

My code should take data from the database, and assign it to an array, but when I list this array, the data does not enter the fields

<?php
include_once '../../classes/Conexao.php';
use classes\Conexao;
header('Content-Type: text/html; charset=iso-8859-1');

$id_veiculo = "1654641106";
$cpf = "25836914766";

$conn = new Conexao();
$link = $conn->getLink();

$select = "SELECT veiculo.PLACA , veiculo.NOME as    NOME_VEICULO,cliente.NOME as NOME_CLIENTE,cliente.TELEFONE FROM `veiculo`  INNER join cliente on veiculo.CPF = cliente.CPF WHERE cliente.CPF =$cpf AND veiculo.ID_CARRO =$id_veiculo ";
$resultado_consulta = mysqli_query($link, $select);

while( $row = mysqli_fetch_assoc($resultado_consulta) )
{
 echo $row['NOME_CLIENTE'];
 echo "<BR>";
 echo $row['NOME_VEICULO'];
 echo "<BR>";
 echo $row['TELEFONE'];
 echo "<BR>";
 echo $row['PLACA'];
}
$resultado[] = array(
 'nome_cliente' =>$row['NOME_CLIENTE'],
 'cpf' =>$cpf,
 'telefone' =>$row['TELEFONE'],
'nome_veiculo' =>$row['NOME_VEICULO'],
 'placa' =>$row['PLACA'],
 );
echo(json_encode($resultado));
?>

This listing I did just to make sure that the query was working, and is, but the assignment to the array does not run. the result of this code is like this[1] Note that the value of the $Cpf variable that was started at the beginning of the code is assigned, but the result of the query is not

1 answer

0


Possibly because you are setting the array out of the loop while(), try like this:

while( $row = mysqli_fetch_assoc($resultado_consulta) )
{
    $resultado[] = array(
     'nome_cliente' =>$row['NOME_CLIENTE'],
     'cpf' =>$cpf,
     'telefone' =>$row['TELEFONE'],
    'nome_veiculo' =>$row['NOME_VEICULO'],
     'placa' =>$row['PLACA'],
     );
}
echo(json_encode($resultado));
  • 1

    Yeah, after I asked who I saw it was for that, but thanks for the attention you saw

  • Thank you for being able to help

Browser other questions tagged

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