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
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;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.