添加行中的所有数字

时间:2013-07-18 20:46:47

标签: php mysql math

我有一个这样的数据库:

ID    |    AMOUNT 
1           15.00
2          100.00
3          100.00

我需要将所有金额加在一起。我已经尝试了一些PHP数学的东西,但却无法使它工作。

<?php
$total = mysql_query("SELECT amount FROM payments") 
or die(mysql_error()); 

$grandtotal=   
    while($total1 = mysql_fetch_array( $total )) {

$total1['amount']+


};

?>

2 个答案:

答案 0 :(得分:1)

SELECT SUM(amount) as sum_amount FROM payments

答案 1 :(得分:0)

您要完成的具体任务有两种解决方案:

while($total1 = mysql_fetch_array( $total )) {

$total1['amount']++; //returns Returns $total1['amount'], then increments it by one.

} // <-- semicolon removed

正如Mark Ba​​ker在评论中建议的那样,您可以使用SUM在SQL查询中执行此操作:

SELECT SUM(amount) AS amount_sum FROM payments