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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵