Return with json array showing only first record

Asked

Viewed 406 times

1

<?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 it is only returning the first one, as it could return all ?

Current return:

{"result":true,"dados":{"endereco":"RUA ABEL COUTO"}}

2 answers

2


Missed you put the: $obj = $result->fetch_object() within a loop to go through all the records of select. I also added the result array to receive all 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);
?>
  • Only in php is not returning anything on the screen, but if I give a var_dump($result); it has the results, strange, ta missing something minimal

  • If you access this php page in the browser it is empty?

  • yes, more if in php I include var_dump($result); it returns: array(1873) { [0]=> array(2) { ["result"]=> bool(true) ["data"]=> array(1) { ["address"]=> string(14) "STREET ABEL COUTO" } [1]=> array(2) { ["result"]=> bool(true) ["data"]=> array(1) { ["address"]=> string(15) "STREET OF ACACIAS" } } .....

  • yes pq result is an array ai gets that way. Ai qnd vc uses the echo json_encode($resultado); it turns the array to json. But it was to show the json on the php page.

  • Solved: I put json inside your while

  • I supplemented your reply

Show 1 more comment

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 as :

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

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


You need to do 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';
  • It happened the same way the @Jessika help in php is not returning anything on the screen, more if I give a var_dump($result); or in your case var_dump($arr); it has the results

Browser other questions tagged

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