【发布时间】:2013-09-11 00:22:58
【问题描述】:
我想根据 SQL 查询的结果创建一个 JSON 表。我在 phpMyAdmin 上尝试了查询,它是正确的(我得到了我想要的数据)但是当我尝试使用下面的代码将其转换为 JSON 表时,结果是一个具有正确结构但不是值的表.
/* 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);
【问题讨论】:
-
对不起,任何令人畏惧的术语,我对此很陌生。 JSON字符串就是我想说的。
标签: php mysql sql json phpmyadmin