将动态列Mysql转换为JSON

时间:2015-11-28 03:45:07

标签: php mysql json pivot-table

Heiii我有一些问题,我有一个动态查询/数据透视表mysql,我想将此查询转换为json,这是我的脚本php

$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$supervisor='ganda.padeardja';
//-- query
$multi="
SET @SQL = NULL;

SELECT
GROUP_CONCAT(
DISTINCT CONCAT(
'MAX(IF(salesman = ''',
salesman,
''', total_aktivitas, 0)) AS `',
salesman,
'`'
)
) INTO @SQL
FROM
v_dashboard_supervisor_activity2
WHERE
supervisor LIKE '%ganda.padeardja%';


SET @SQL = CONCAT(
'SELECT v_calendar.cdate as aktivitas,',
@SQL,
' FROM v_dashboard_supervisor_activity2 RIGHT JOIN v_calendar ON v_calendar.cdate=v_dashboard_supervisor_activity2.tgl_aktivitas WHERE cdate BETWEEN CURDATE() - INTERVAL 6 DAY AND CURDATE() GROUP BY cdate ORDER BY cdate ASC '
);

PREPARE stmt
FROM
@SQL;

EXECUTE stmt;

";
if($result=$db->multi_query($multi)) {
$i=0;
do{
$db->next_result();
if(++$i==5)$result=$db->store_result();
}while($db->more_results() && $i<5);
}
$data=array();
$i=0;
print_r($db->error_list);
while($meta=$result->fetch_field()){
$data[$i]=array();
$data[$i]['name']=$meta->name;
$data[$i++]['data']=array();
}
$result->data_seek(0);
while($datas=$result->fetch_row()){
$i=0;
foreach($datas as $d){
$data[$i++]['data'][]=$d;
}
}
echo json_encode($data);

但结果是:

  

Array()

     

致命错误:在非对象上调用成员函数fetch_field()   在第53行的D:\ xampp \ htdocs \ test \ index_try2.php

我预期的结果是这样的:

[{"name":"aktivitas","data":`["2015-11-22","2015-11-23","2015-11-24","2015-11-25","2015-11-26","2015-11-27","2015-11-28"]},

{"name":"abdul.qodir","data":["0","11","4","6","2","6","1"]},{"name":"andrinanta.putra","data":["0","4","7","6","5","0","0"]},{"name":"ganda.sls","data":["0","0","0","0","0","0","0"]},{"name":"paulus.jatmiko","data":["0","5","5","4","5","5","0"]},{"name":"sandy.wibowo","data":["0","0","0","6","5","12","0"]}]

你可以帮我解决我犯错的地方吗?

0 个答案:

没有答案