嗨,我在oracle数据库中有一个表。它有purchase
sale
income
列。
现在,我想在sum
列中显示这些值的INCOME
,并在php页面中显示总收入为sum[INCOME]
。我试图使用函数查询。查询似乎不起作用。这是代码。
<?php
function formatMoney($number, $fractional=false) {
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
$c = oci_connect('SYSTEM', 'passward', 'db');
$result1 = oci_parse($c,"SELECT sum(INCOME) FROM TABLE WHERE SALE_DATE='$rdt'");
oci_execute($result1);
$row = oci_fetch_array($result1, OCI_ASSOC);
while($row = oci_fetch_array($result1, OCI_ASSOC))
{
$total=$row['sum(INCOME)'];
echo formatMoney($total, true);
}
?>
答案 0 :(得分:0)
这是不正确的,使用关键字as
:
$result1 = oci_parse($c,"SELECT sum(INCOME) as total_income FROM TABLE WHERE SALE_DATE='$rdt'");
然后你就可以像以下一样获取它:
$total=$row['total_income'];
或者你可以让查询保持不变,而不是使用:
获取它$total=$row[0]; //aggregate functions return only single row, so this
//basically means fetch the first row