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 Link to comment https://forums.phpfreaks.com/topic/131343-solved-add-row-values/ 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 Link to comment https://forums.phpfreaks.com/topic/131343-solved-add-row-values/#findComment-682033 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; Link to comment https://forums.phpfreaks.com/topic/131343-solved-add-row-values/#findComment-682037 Share on other sites More sharing options...
phpretard Posted November 4, 2008 Author Share Posted November 4, 2008 Thank a bunch! Link to comment https://forums.phpfreaks.com/topic/131343-solved-add-row-values/#findComment-682040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.