ngreenwood6 Posted October 13, 2008 Share Posted October 13, 2008 I have a real quick question. I am trying to find the sum of a row in the database. However am having a little trouble with it. I am trying to find the sum of a row called amount. I need to put that into a variable so that I can echo it. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
aebstract Posted October 13, 2008 Share Posted October 13, 2008 You need to add the value in each row of amount to get a grand total of all the rows? Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Are you using the MySQL SUM function? And I hope you mean Field and not Row. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 Yeah that is what I am trying to do the field sorry. I am not sure the proper way to use the sum function and how to get it to a variable an example would be great. Quote Link to comment Share on other sites More sharing options...
redarrow Posted October 13, 2008 Share Posted October 13, 2008 <?php // Make a MySQL Connection $query = "SELECT type, SUM(price) FROM products GROUP BY type"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "Total ". $row['type']. " = $". $row['SUM(price)']; echo "<br />"; } ?> http://www.tizag.com/mysqlTutorial/mysqlsum.php Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I have this code but it is giving me an error with the syntax. $new_query = "SELECT *,SUM(amount) FROM $banking_table $limit"; Any help is appreciated. It worked before I added the sum(amount) portion. EDIT: Maybe I shoudl have read better because he did give the link. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 echo $new_query and post what it displays post the error Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I am getting this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\banking.php on line 86 Line 86 is the query and when I echo the query it shows this: SELECT *,SUM(amount) FROM banking LIMIT 10,10 any ideas Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Line 86 can't be the Query, the error is on the mysql_fetch_array line, which means you may have forgot to actually do the query. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I am sorry line 86 is this sorry I was still on the other part: while($row = mysql_fetch_array($new_results)) Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Post the code from the start to line 90. You have a connection to the DB right? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 Yeah I have a connection to the database. Like I said the rest of the code is right because if i take out the sum part of it, it works fine. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Use mysql_error() after the query then to find out why it's failing. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I am getting this error: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Does SELECT SUM(amount) FROM banking LIMIT 10,10 work? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 Yeah it looks like that is working, but I need it that query to also get the results. Because now all it is showing is the sum but not the actual entries. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 You have to use the Group By parameter if you use more than 1 field in your select. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I tried using the group by like this: $new_query = "SELECT *,SUM(amount) FROM $banking_table GROUP BY id $limit"; but it does not actually sum it. It just displays the same as the last entry. Any ideas? Quote Link to comment Share on other sites More sharing options...
redarrow Posted October 13, 2008 Share Posted October 13, 2008 <?php $sql="SELECT SUM(amount) AS got_it FROM banking LIMIT 10,10"; $res=mysql_query($sql)or die(mysql_error); while($f=mysql_fetch_assoc($res)){ echo" <br><br> Amount: {$f['amount']} : Sum: {$f['got_it']} "; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 That doesn't help him with his question though. My advice, have a 2nd query just for the SUM. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 13, 2008 Author Share Posted October 13, 2008 I did end up making a second query, but is there a way to do it? Another question you may be able to answer is with adding queries will this slow my website down? 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.