Hi I am having problem generating a json object that will return branch, cf of that month under 1 array like this:
Desired Output
[{
"month": "Jan",
"Branch2": 550965.96 ,
"Branch1": 663134.44
}, {
"month": "Feb",
"Branch2": 472793.10,
"Branch1": 492784.54
}, {
"month": "March",
"Branch2": 394616.65,
"Branch1": 757639.93
}, {
"month": "April",
"Branch2": 376403.65 ,
"Branch1": 569404.61
}]
Here is my current Code
<?php
$dbhost = 'localhost';
$dbname = 'spmdb';
$dbuser = 'root';
$dbpass = '';
try{
$dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $ex){
die($ex->getMessage());
}
$stmt = $dbcon->prepare("SELECT month.monthname,coll.branch,coll.cf FROM coll INNER JOIN month ON month.id = coll.`month` group by coll.month,coll.branch");
$stmt->execute();
$row1 = [];
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$row1[]= ['month' => $monthname, $branch => $cf];
}
echo json_encode($row1,JSON_NUMERIC_CHECK);
?>
and currently ouputs this:
[{"month":"Jan","Branch4":40275}
,{"month":"Feb","Branch5":165173.91}
,{"month":"Feb","Branch1":93360.33}
,{"month":"Apr","Branch1":65415.75}]
Is there a way I could do this without modifying the mysql ?
GROUP BYwhen you have no aggregation functions likeSUM()orMAX()?