Problems with Json and php

1

My code should take data from the database, and assign it to an array, however, when this array is ready, 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 just did to make sure the query was working, and it is, but the assignment to the array does not run. the result of this code looks like this [ Note that the value of the $ cpf variable that was started at the beginning of the code is assigned, but the query result is not

    
asked by anonymous 15.05.2018 / 00:54

1 answer

0

Possibly because you are setting the array outside the while() loop, try:

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));
    
15.05.2018 / 00:59