Jump to content

[SOLVED] Add Row Values


phpretard

Recommended Posts

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

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;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.