通过php查询添加oracle数据库表的列值

时间:2014-03-17 07:11:50

标签: php oracle

嗨,我在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);
 }

?>

1 个答案:

答案 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