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? Quote Link to comment 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; ?!? Quote Link to comment 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 :-\ Quote Link to comment 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; Quote Link to comment 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 Quote Link to comment 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"; Quote Link to comment Share on other sites More sharing options...
PJ droopy pants Posted January 8, 2009 Author Share Posted January 8, 2009 Thank you Quote Link to comment Share on other sites More sharing options...
corbin Posted January 9, 2009 Share Posted January 9, 2009 No problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.