11-30-2016, 01:41 AM
For some reason, I'm a big fan of foreach loops. If you do the sum in a query, it's going to come back as an object and actually the total will be represented as a string, which is a little bit of a pain. I tend to foreach loop it...
If you want to do the sum in the query, you were really close.
Then you can see your results with
Code:
$usageQ = $db->query("SELECT month_usage FROM devices");
Code:
$usage = $usageQ->results();
Code:
foreach ($usage as $u) {
Code:
$sum+= $u->month_usage;
Code:
}
Code:
echo $sum;
If you want to do the sum in the query, you were really close.
Code:
$usageQ = $db->query("SELECT SUM(month_usage) FROM devices");
Code:
$usage = $usageQ->results();
Code:
dump($usage);