这是我用来尝试计算“amountReceived”列总和的代码。 invID'95'的总数应该= 312,但由于某种原因它只是= 300.我不确定是什么问题。 (请参阅下面的DB图像)。
$sql = mysql_query("SELECT amountReceived FROM payments WHERE invID='".$id."'");
$result = mysql_fetch_array($sql);
$totalPayment = array_sum($result);
$balanceDue = $invoiceTotal - $totalPayment;
答案 0 :(得分:7)
如何使用SUM()?
$sql = mysql_query("SELECT SUM(amountReceived) FROM payments WHERE invID='".$id."'");
$totalPayment = mysql_fetch_row($sql);
答案 1 :(得分:3)
mysql_fetch_array($ sql)返回一个数组,其中包含您获取的一行的所有“列”,意味着您获得了一个阵列( 'amountReceived'=> 150)
这应该有效:
$sql = mysql_query("SELECT amountReceived FROM payments WHERE invID='".$id."'");
$totalPayment = 0;
while($result = mysql_fetch_array($sql)) {
$totalPayment += $result['amountReceived'];
}
$balanceDue = $invoiceTotal - $totalPayment;
这只是问题的php端描述,如果你想提高效率,请使用sel的版本;)
答案 2 :(得分:0)