PJ droopy pants Posted January 8, 2009 Share Posted January 8, 2009 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 More sharing options...
mada9369 Posted January 8, 2009 Share Posted January 8, 2009 $result = mysql_query("SELECT * FROM `users` ORDER BY `id` ASC"); $totalusers = mysql_num_rows($result); while($row = mysql_fetch_array($totalusers)){ $money = $row['money']; echo $money; ?!? Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732848 Share on other sites More sharing options...
PJ droopy pants Posted January 8, 2009 Author Share Posted January 8, 2009 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource :-\ Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732896 Share on other sites More sharing options...
corbin Posted January 8, 2009 Share Posted January 8, 2009 This is MySQL related more so than PHP Math.... Anyway, to get the sum of the entire table: SELECT SUM(money) FROM users; Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732898 Share on other sites More sharing options...
PJ droopy pants Posted January 8, 2009 Author Share Posted January 8, 2009 I tried $result = mysql_query("SELECT SUM(money) FROM `users'"); $totalmoney = mysql_fetch_array($result); but getting a Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732917 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 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"; Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732922 Share on other sites More sharing options...
PJ droopy pants Posted January 8, 2009 Author Share Posted January 8, 2009 Thank you Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-732932 Share on other sites More sharing options...
corbin Posted January 9, 2009 Share Posted January 9, 2009 No problem. Link to comment https://forums.phpfreaks.com/topic/140066-solved-totaling-sum-of-a-field/#findComment-733715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.