从SQL查询创建JSON表 [英] Creating JSON table from SQL query
本文介绍了从SQL查询创建JSON表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据SQL查询的结果创建JSON表.我在phpMyAdmin上尝试了查询,它是正确的(我得到了我想要的数据),但是当我尝试使用下面的代码将其转换为JSON表时,结果是具有正确结构但表中没有值的表
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);
推荐答案
phpMyAdmin允许以JSON格式导出,也许可以为您提供帮助.
phpMyAdmin permits to export in JSON format, maybe this can help you.
这篇关于从SQL查询创建JSON表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文