phpretard Posted November 4, 2008 Share Posted November 4, 2008 I am trying to add row values from my database. I am sure this is simple but I am strugling to figure out the code. I have multiple rows named "Question2_1" each with a numeric value. I would like to take all of them and add them together in one query. So if I had 5 rows each with a value of 2 the $total would be 10 Here is what I have. $Question2_1=mysql_query("SELECT * FROM survey_software WHERE Question2_1!=''"); while($row = mysql_fetch_array($Question2_1)) { $totalQ2_1=$row['Question2_1']; echo $total; // <<<<<<<<<<<<<PROBLEM } Thank you Quote Link to comment Share on other sites More sharing options...
revraz Posted November 4, 2008 Share Posted November 4, 2008 Have you looked into the GROUP BY parameter of MySQL? http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 modifying your code: $totalQ2_1 = 0; $Question2_1=mysql_query("SELECT * FROM survey_software WHERE Question2_1!=''"); while($row = mysql_fetch_array($Question2_1)) { $totalQ2_1 += $row['Question2_1']; } echo $totalQ2_1; the better way: $Question2_1 = mysql_query("SELECT SUM(Question2_1) FROM survey_software WHERE Question2_1!=''"); list($totalQ2_1) = mysql_fetch_array($Question2_1); echo $totalQ2_1; Quote Link to comment Share on other sites More sharing options...
phpretard Posted November 4, 2008 Author Share Posted November 4, 2008 Thank a bunch! 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.