Fill array with mysqli_query

0

I want to fill an array with a query that I make from a table but I do not know how to format it correctly.

This is my PHP:

$consulta="SELECT * FROM registros2";
$registros=mysqli_query($conexion,$consulta) or die ("Problemas con la consulta");
while ($reg=mysqli_fetch_array($registros))
{
array_push($c,
$reg['id'],$reg['cliente'],$reg['metodo_pago'],$reg['hora'],$reg['fecha_entrega']);
}

When I print the result it looks like this:

[
   "100",
   "Carlos Luna",
   "Credito 30D",
   "14:15:00",
   "2018-05-09",
   "104",
   "sadfsadf",
   "Contado",
   "12:32:00",
   "2018-06-18"
]

And I need the format to be like this:

{
   "data": [
     [
       "100",
       "Carlos Luna",
       "Credito 30D",
       "14:15:00",
       "2018-05-09"
     ],
     [
       "104",
       "sadfsadf",
       "Contado",
       "12:32:00",
       "2018-06-18"
     ]
   ]
}

Thank you in advance for your answers.

    
asked by Carlos Roberto Luna Ochoa 19.06.2018 в 18:37
source

2 answers

0

You can try this code

     $c = [
         'data' => []
     ];
     $consulta="SELECT * FROM registros2";
     $registros=mysqli_query($conexion,$consulta) or die ("Problemas con la consulta");
     while ($reg=mysqli_fetch_array($registros))
     {
         array_push($c['data'],[$reg['id'],$reg['cliente'],$reg['metodo_pago'],$reg['hora'],$reg['fecha_entrega']]);
     }

You tell me if it worked well for you :)

    
answered by 19.06.2018 / 18:57
source
0

You have to make each record an array. Try it like this:

$consulta="SELECT * FROM registros2";
$registros=mysqli_query($conexion,$consulta) or die ("Problemas con la consulta");
$c = array('data' => array());
while ($reg=mysqli_fetch_array($registros))
{
array_push($c['data'],
array($reg['id'],$reg['cliente'],$reg['metodo_pago'],$reg['hora'],$reg['fecha_entrega']));
}
    
answered by 19.06.2018 в 18:44