Reputation: 295
I am trying to print with JSON a SUM() of a price.
Currently I am trying:
$query="SELECT SUM(cost) FROM `Service`";
$result = mysql_query($query);
$json = array();
while($row = mysql_fetch_array($result))
{
$json['cost'] = $row['cost'];
}
print json_encode($json);
mysql_close();
This returns null.
If I try SELECT cost FROM Service
instead, it returns the last cost from the database.
What Im I doing wrong?
Upvotes: 0
Views: 403
Reputation: 263943
supply an ALIAS
on the column passed on the aggregate function
SELECT SUM(cost) totalCOST FROM `Service`
so you can fetch the columnName
$json['cost'] = $row['totalCOST'];
Upvotes: 2