duanliang1999 2014-03-17 07:11
浏览 37
已采纳

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

Hi, I have a table in oracle database. It has columns purchase sale income.

Now I want to show sum of those values in INCOME column and show in php page that total income is sum[INCOME]. I tried to use a query with function. query seems not working. Here is the code.

<?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条回答 默认 最新

  • douchao1957 2014-03-17 07:16
    关注

    That's incorrect, give the aggregate function an alias using the keyword as:

    $result1 = oci_parse($c,"SELECT sum(INCOME) as total_income FROM TABLE WHERE SALE_DATE='$rdt'");
    

    And then you can fetch it like:

    $total=$row['total_income'];
    

    Or you can let the query remain the same and instead fetch it with:

    $total=$row[0]; //aggregate functions return only single row, so this
                    //basically means fetch the first row
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符