【问题标题】:error with oci_fetch_array, can retrieve data from oracle by phpoci_fetch_array 出错,可以通过 php 从 oracle 检索数据
【发布时间】:2011-04-15 20:44:41
【问题描述】:

当我尝试循环获取数据时,我可以通过 php 从 oracle db 中检索数据,但它给了我错误

未定义索引:第 15 行 C:\xampp\htdocs\testing\test.php 中的 id

这是我的代码

<?php
$username = "kemo";
$password = "kemoacer77";
$server = "localhost/XE";
$conn = oci_connect($username, $password, $server);
if(!$conn){
die("connect error".oci_error());
}

$stid = oci_parse($conn, 'SELECT id, username FROM users');
oci_execute($stid);

while (($row = oci_fetch_array($stid, OCI_BOTH))) {
    // Use the uppercase column names for the associative array indices
    echo  $row['id'] ;
    echo $row['username'];
}

oci_free_statement($stid);
oci_close($conn);


?>

【问题讨论】:

    标签: php oracle


    【解决方案1】:

    oci_fetch_array() 的文档说:

    Oracle 的默认值,不区分大小写 列名将大写 结果中的关联索引 数组。区分大小写的列名 将使用数组索引 确切的列大小写。
    使用 var_dump() 在结果数组上 验证要用于的适当案例 每个查询。

    你的代码中的注释也说:

    // Use the uppercase column names for the associative array indices
    


    那么,为什么要使用小写的列名?

    这是你的代码:

    echo  $row['id'] ;
    echo $row['username'];
    

    根据您代码中的注释和手册中的注释,您是否应该不使用大写,如下所示:

    echo  $row['ID'] ;
    echo $row['USERNAME'];
    


    而且,如果这仍然不起作用,请按照手册中的说明进行操作:在循环中使用 var_dump(),查看数据的外观:

    while (($row = oci_fetch_array($stid, OCI_BOTH))) {
        var_dump($row);
    }
    

    【讨论】:

    • 当用户名、id 改为大写字符 USERNAME、ID 运行良好时,哇现在好了
    • 但是我如何检查查询在 php + mysql 中是否有错误我使用这种方式if(!$query){die(mysql_error())} 如何使用 oracle 来做到这一点
    • 看来 oci_execute() 在失败时返回 false :php.net/oci_execute
    • 好吧,如果 SQL 查询中有错误,它就会失败——我想那个函数会返回 false (只要使用一些无效的 SQL 查询,你就会知道: -) )
    【解决方案2】:

    在我的例子中,我使用了 NVL

    Oracle/PLSQL NVL 函数允许您在遇到空值时替换一个值

    NVL(string1, replace_with)

    从销售额中选择 NVL(佣金,0);

    输出:

    123 223 323

    423

    答案是这样的 123 223 323 0 423

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 2018-08-19
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 2016-02-07
      相关资源
      最近更新 更多