0

我有一个这样的数据库:

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']+


};

?>
4

2 回答 2

1
SELECT SUM(amount) as sum_amount FROM payments
于 2013-07-18T20:49:40.703 回答
0

对于您要完成的特定任务,有两种解决方案:

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

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

} // <-- semicolon removed

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

SELECT SUM(amount) AS amount_sum FROM payments
于 2013-07-18T20:51:21.763 回答