Return with json array showing only first record

0
<?php
    //database configuration
    $dbHost     = '127.0.0.1';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'master';

//connect with the database
$Mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

$arr = array();


//get matched data from skills table
if(empty($erro)){
    $query = "SELECT 
    r.endereco_cep        AS CEP,
    r.endereco_logradouro AS ENDERECO, 
    b.bairro_descricao    AS BAIRRO,
    c.cidade_codigo       AS CODCID,
    c.cidade_descricao    AS CIDADE,
    e.uf_sigla            AS SIGLA,
    e.uf_descricao        AS ESTADO
    FROM 
    cep_endereco AS r
    LEFT JOIN cep_bairro AS b ON b.bairro_codigo = r.bairro_codigo
    LEFT JOIN cep_cidade AS c ON c.cidade_codigo = b.cidade_codigo
    LEFT JOIN cep_uf     AS e ON e.uf_codigo = c.uf_codigo   
    WHERE 
    c.cidade_codigo = '3169'";
         $result = $Mysqli->query($query);

         if($result->num_rows > 0){

            $obj = $result->fetch_object();

            $arr['result'] = true;
            $arr['dados']['endereco']= $obj->ENDERECO;



         }else{
            $arr['result'] = false;
            $arr['msg'] = "USUARIO INCORRETO";
         }
      }else{
         $arr['result'] = false;
         $arr['msg'] = $erro;
      }

     echo json_encode($arr);
?>

In the above query the intention is to return all addresses in a json, but this is only returning the first one, how could it all return?

  

Return current:

{"result":true,"dados":{"endereco":"RUA ABEL COUTO"}}
    
asked by anonymous 11.04.2017 / 14:54

2 answers

1

You have missed the: $obj = $result->fetch_object() within a loop to go through all the select registers. I also added the result array to get all the results.

  <?php
    //database configuration
    $dbHost     = '127.0.0.1';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'master';

//connect with the database
$Mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

$arr = array();
$resultado = array();


//get matched data from skills table
if(empty($erro)){
    $query = "SELECT 
    r.endereco_cep        AS CEP,
    r.endereco_logradouro AS ENDERECO, 
    b.bairro_descricao    AS BAIRRO,
    c.cidade_codigo       AS CODCID,
    c.cidade_descricao    AS CIDADE,
    e.uf_sigla            AS SIGLA,
    e.uf_descricao        AS ESTADO
    FROM 
    cep_endereco AS r
    LEFT JOIN cep_bairro AS b ON b.bairro_codigo = r.bairro_codigo
    LEFT JOIN cep_cidade AS c ON c.cidade_codigo = b.cidade_codigo
    LEFT JOIN cep_uf     AS e ON e.uf_codigo = c.uf_codigo   
    WHERE 
    c.cidade_codigo = '3169'";
         $result = $Mysqli->query($query);

         if($result->num_rows > 0){

            while ($obj = $result->fetch_object()){

               $arr['result'] = true;
               $arr['dados']['endereco']= $obj->ENDERECO;
               $resultado[] = $arr;
            }

         }else{
            $arr['result'] = false;
            $arr['msg'] = "USUARIO INCORRETO";
            $resultado[] = $arr;
         }
      }else{
         $arr['result'] = false;
         $arr['msg'] = $erro;
         $resultado[] = $arr;
      }

     echo json_encode($resultado);
?>
    
11.04.2017 / 15:04
1
if($result->num_rows > 0){
    $obj = $result->fetch_object();
    $arr['result'] = true;
    $arr['dados']['endereco']= $obj->ENDERECO;
}else{
    $arr['result'] = false;
    $arr['msg'] = "USUARIO INCORRETO";
}

echo json_encode($arr);

In your code, the loop is doing the same thing as:

$arr['dados']['endereco']= 'endereco 1';
$arr['dados']['endereco']= 'endereco 2';
$arr['dados']['endereco']= 'endereco 3';

That is, it is always overlapping the last key. Its output {"result":true,"dados":{"endereco":"RUA ABEL COUTO"}} corresponds to the last line of the loop

You need to make an array as follows in your loop:

$arr[]['result'] = true;
$arr[]['dados']['endereco']= $obj->ENDERECO;

The final output would be:

$arr[0]['dados']['endereco']= 'endereco 1';
$arr[1]['dados']['endereco']= 'endereco 2';
$arr[2]['dados']['endereco']= 'endereco 3';
    
11.04.2017 / 15:25