Jim McDermott
Jim McDermott

Reputation: 255

Adding inside a while loop php

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.

Upvotes: 0

Views: 53

Answers (1)

Marty
Marty

Reputation: 39456

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

SELECT SUM(value) FROM pointvalues WHERE code = x

Reference: SUM

Upvotes: 4

Related Questions