0

I have a script that pulls values from a MySQL database and puts into into a variable. Each time it iterates, it pulls a new value. I want to add these values together. Here's what I have:

while ($row = mysql_fetch_array( $result )) {

$getvalue = mysql_query("SELECT value FROM pointvalues WHERE code = '$row[code]'")or die(mysql_error()); 
$rowcode1 = mysql_fetch_array($getvalue);
$finalValue = $rowcode1["value"];
}  

I want to keep adding $finalValue to itself.

1
  • There is a += operator for that. But it's quicker to ask your RDBMS to do it. Commented Sep 17, 2014 at 1:43

1 Answer 1

4

I recommend saving yourself all that work and simply changing your query to:

SELECT SUM(value) FROM pointvalues WHERE code = x

Reference: SUM

Sign up to request clarification or add additional context in comments.

1 Comment

When I do this, I get "Resource id #7Resource id #8." Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.