The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Total Sum of Column
#1
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:

Code:
$usage = $db->query("SELECT SUM(month_usage) FROM devices");
Code:
$row = mysql_fetch_assoc($usage);
Code:
$sum = $row['month_usage'];

I think I am mixing up my queries because I am getting this error:

Quote: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?
  Reply
#2
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...

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();
Then you can see your results with
Code:
dump($usage);
  Reply
#3
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 Tongue

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 Smile
  Reply
#4
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.
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)