This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

Total Sum of Column

In UserSpice 4.3 and Below · Started by winterswolf on 2016-11-29 11:56 pm · 9083 views · 3 replies

Hey guys,

So I think I am having a bit of a blonde moment. In one of my database tables I have a column that is numbers, I want to query this table and get the sum of all the numbers in this column.

I'm having trouble with mysql_fetch_array / mysql_fetch_assoc. Is there another way userspice handles this?

Here's my code so far:
$usage = $db->query("SELECT SUM(month_usage) FROM devices");
$row = mysql_fetch_assoc($usage);
$sum = $row['month_usage'];

I think I am mixing up my queries because I am getting this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in /#PATH#/dashboard.php on line 13

I have another version of this that uses mysql_fetch_array in a different manner, but that basically gives the same error.

Anyone got any ideas on where to go from here?
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...
$usageQ = $db->query("SELECT month_usage FROM devices");
$usage = $usageQ->results();
foreach ($usage as $u) {
$sum+= $u->month_usage;
}
echo $sum;

If you want to do the sum in the query, you were really close.
$usageQ = $db->query("SELECT SUM(month_usage) FROM devices");
$usage = $usageQ->results();
Then you can see your results with
dump($usage);
Awesome, that's exactly the kind of answer I was looking for!

Been quite a while since I have done anything with PHP or databases outside of "grab this db table and make it a html table". Still getting stumped on simple things and then feeling silly afterwards :P

Really enjoying UserSpice btw, I have used UserFrosting and UserCake before but they didn't quite tickle my fancy for basing entire projects on it like UserSpice does. Currently using it to build a dashboard that will set up VPN connections and upload modem configurations to modems around the world remotely with a mixture of PHP and an ASP.Net Server applet. Because of this security is crucial, wanted something where security was a focus and have something I could build on top of. That's how I found this project, was looking at CSRF vulnerabilities and came across your post. Keep up the good work! Already have a few other projects in mind UserSpice is going to dramatically reduce work required for :)
Thanks for the encouragement.

I'm actually a rather noob php programmer myself, but I really liked the way UserCake kind of got out of the way. It was outdated and had pretty big security flaws, but I kind of liked the attitude. I've tried all the competitors and I like the things they do, but to be honest, I've had a hard time installing 50{3bc1fe685386cc4c3ab89a3f76566d8931e181ad17f08aed9ad73b30bf28114d} of them before I even tried to use them.

Anything I can do to help, let me know.