Jump to content

Loop though DB adding values


joecooper

Recommended Posts

Hi,

 

I have a database with some rows. in each row there is a number such as: 0.0023 or 0.0135...

 

I have the following code to loop though them and add the numbers up but it always returns 0?

 

$sql="SELECT * FROM tickets WHERE `round` = '$round' AND `confirmed` = 'yes' ORDER BY id ASC";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

while($row = mysql_fetch_array( $result )) {
   $balance = $balance + $row['amount'];
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/245648-loop-though-db-adding-values/
Share on other sites

Is the amount column a float?

 

$sql="SELECT SUM(`amount`) AS total FROM tickets WHERE `round` = '$round' AND `confirmed` = 'yes' ORDER BY id ASC";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

while($row = mysql_fetch_array( $result )) {
   echo $row['total'];
}

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.