I am attempting to use JSON to print the total sum of a price.
Here is my current approach:
$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();
However, this is returning null.
If I change the query to SELECT cost FROM Service
, it only returns the last cost from the database.
Could someone please point out what mistake I am making here?