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 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码