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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP