2013-07-18 55 views
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']+ 


}; 

?> 
+1

嘗試'$ total1 ['amoun t'] ++'並在最後一個大括號後面刪除分號。 –

+0

爲什麼不這樣做在SQL查詢 –

+1

'SELECT SUM(amount)AS amount_sum FROM payments' –

回答

1
SELECT SUM(amount) as sum_amount FROM payments 
0

有針對特定任務兩種解決方案,你要完成:

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

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

} // <-- semicolon removed 

正如馬克·貝克在評論建議,你可以從你的SQL中做查詢使用SUM

SELECT SUM(amount) AS amount_sum FROM payments