how can I get the following json file from a php query - sqlserver

0

I have the following query

$query="SELECT nombre,edad,genero FROM ...";
$result = sqlsrv_query($_conexion,$query) or die('Error en la consulta');
while($row = sqlsrv_fetch_array($result)) {
    extract($row);

    $mundos['data'][]=
         array(
                $nombre,
                $edad,
                $genero 
          );

    }

    print_r(json_encode($mundos));

However, nothing appears on the screen, if I put single quotes in the age and gender achievement data it appears as follows.

{"data":[["luis","$edad","$genero"],["maria","$edad","$genero"]

.. and so on.

The output format I require is the following:

{"data":[
    ['luis',"25","hombre"],
    ["maria","30","mujer"],
    ["karla","18","mujer"],
    ["pepe","35","hombre"]


  ]
}
    
asked by L. Moreno 11.04.2017 в 06:52
source

2 answers

0

Do not put it like this:

$mundos['data'][]=
     array(
            $nombre,
            $edad,
            $genero 
      );

}

I would believe that changing it here is enough:

$mundos['data'] = array(/*Aqui tus datos*/)

and then a echo json_encode($mundos);

    
answered by 11.04.2017 в 17:24
0

It's simple you should learn a little more about associative arrays

    $mundos['data'][]=
     array(
            'nombre' => $nombre,
            'edad' => $edad,
            'genero' =>$genero 
      );
    
answered by 11.04.2017 в 17:31