dongzuo4666 2018-11-16 06:33
浏览 135

OCI_BIND_BY_NAME中的异常行为

I was using the following code to bind variables using oci_bind_by_name but getting returned only single row even though multiple rows were available.

<?php 
    $queryArr = array(11423,24242,2463,23434);
    $sqlarr = array();
    for ($i = 0; $i < count($queryArr); $i++) {
        array_push($sqlarr, ":B$i");
    }
    $sqlstr = implode(",", $sqlarr);
    $sql = "SELECT COL1, COL2, STATUS FROM TAB1 WHERE P_KEY = :USR_ID AND S_COL IN (" . $sqlstr . ") ORDER BY STATUS";
    $this->sth = oci_parse($this->con, $sql);
    oci_bind_by_name($this->sth, ":USR_ID", $usrid);
    for ($i = 0; $i < count($queryArr); $i++) {
        $bid = $queryArr[$i];
        oci_bind_by_name($this->sth, ":B$i", $bid);
    }
    oci_execute($this->sth);
?>

After spending 3 hours finding the error in the code I found that the problem is with OCI_BIND_BY_NAME. I changed the above code as below and I was getting all the rows now.

<?php 
    $queryArr = array(11423,24242,2463,23434);
    $sqlarr = array();
    for ($i = 0; $i < count($queryArr); $i++) {
        array_push($sqlarr, ":B$i");
    }
    $sqlstr = implode(",", $sqlarr);
    $sql = "SELECT COL1, COL2, STATUS FROM TAB1 WHERE P_KEY = :USR_ID AND S_COL IN (" . $sqlstr . ") ORDER BY STATUS";
    $this->sth = oci_parse($this->con, $sql);
    oci_bind_by_name($this->sth, ":USR_ID", $usrid);
    for ($i = 0; $i < count($queryArr); $i++) {
    //Changed code --START--
        $bindName = ":B".$i;
        oci_bind_by_name($this->sth, $bindName, $queryArr[$i]);
    //Changed code --END--
    }
    oci_execute($this->sth);
?>

Can someone please explain the reason behind this...?

So as a summary:

When creating a variable by appending string and using it in OCI_BIND_BY_NAME then it's working fine but when I am directly appending string in the function then it's not working correctly. Also, it's not giving any error message. It's getting executed but returning only single row.

  • 写回答

1条回答 默认 最新

  • dplase3140 2018-11-20 05:57
    关注

    Your first example is binding $bid in each iteration, i.e the same variable location (memory address) is reused. See the related example 'Example #3 Binding with a foreach() loop' in the oci_bind_by_name() documentation:

    foreach ($ba as $key => $val) {
    
        // oci_bind_by_name($stid, $key, $val) does not work
        // because it binds each placeholder to the same location: $val
        // instead use the actual location of the data: $ba[$key]
        oci_bind_by_name($stid, $key, $ba[$key]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配