要以json_encode
格式返回数组,对于单行,只需按如下所示推送元素。例如
$query = "SELECT expenses.ttl AS name, expenses.amnt AS amnt FROM expenses";
$result = $con->prepare($query) or die($con->error);
$result->execute();
$data = array();
foreach($result as $row){
$data[] = $row;
}
print json_encode($data)
但是如何将GROUP_CONCAT
分解数组推入此(下一个)数组。例如
$query = "SELECT GROUP_CONCAT(salary.ttl,',',purchase.ttl,',',extra.ttl) AS name,
CONCAT(SUM(salary.amnt),',',SUM(purchase.amnt),',',SUM(COALESCE(extra.amnt,'0'))) AS amnt
FROM salary LEFT JOIN..."
$result = $con->prepare($query) or die($con->error);
$result->execute();
while($row=$result->fetch(PDO::FETCH_ASSOC)){
$name = explode(',',$row['name']);
$amnt = explode(',',$row['amnt']);
//how to push these specific value to array $data As above.
}
print json_encode($data)