I want to create a JSON table from the results of a SQL query. I tried the query on phpMyAdmin and it is correct (I get the data that I want) but then when I try to convert it to a JSON table using the code below, the result is a table with the correct structure but non of the values.
/* select all moches from the table moches */
$query="SELECT municipio, SUM(moche) AS moche FROM moches GROUP BY municipio";
$result = $mysqli->query($query);
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Municipio', 'type' => 'string'),
array('label' => 'Cantidad total en moches', 'type' => 'number')
);
foreach($result as $r) {
$temp = array();
//Create the different states
$temp[ ] = array('v' => (string) $r['municipio']);
// Total de moches
$temp[ ] = array('v' => (int) $r['moche']);
$rows[ ] = array('c' => $temp);
}
$table['rows'] = $rows;
// convert data into JSON format
$jsonTable = json_encode($table);