Jump to content

[SOLVED] Totaling "sum" of a field


PJ droopy pants

Recommended Posts

I am trying to get a total money from the users database and am having a hard time. I know how to get the total users by doing this..

$result = mysql_query("SELECT * FROM `users` ORDER BY `id` ASC");
$totalusers = mysql_num_rows($result);

 

but how do I total their `money` from that field in the table?

Link to comment
https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/
Share on other sites

You've mixed your quote types in your query. Backticks can be used to demarcate fields and table names whilst standard quotes are used to delimit strings:

 

$result = mysql_query("SELECT SUM(money) FROM `users`") or trigger_error(mysql_error(),E_USER_ERROR);
$sum = mysql_result($result,0);
echo "Sum of the rows is $sum";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.