PHP:如何使用不同的参数值多次调用oci_execute?

时间:2018-02-07 21:12:23

标签: php oracle oci

我有一个Oracle程序需要调用3次。该过程需要5个参数。 3个参数每次都具有相同的值,而其他2个参数每次执行时都有不同的值。如何在每次调用oci_execute之前更改参数值?

这是我现在拥有的:

$sql = "BEGIN
    pkg.DoSomething(p_first => :p_first,
        p_second => :p_second,
        p_third => :p_third,
        p_fourth => :p_fourth,
        p_fifth => :p_fifth);
    COMMIT;
    END;";

$statement = oci_parse($conn, $sql);
oci_bind_by_name($statement, ":p_first", $one);
oci_bind_by_name($statement, ":p_second", $two);
oci_bind_by_name($statement, ":p_third", $three);

$fourthVals = array("a", "b", "c");
$fifthVals = array("x", "y", "z");

for ($index = 0; $index < 3; $index++) {
    oci_bind_by_name($statement, ":p_fourth", $fourthVals[$index]);
    oci_bind_by_name($statement, ":p_fifth", $fifthVals[$index]);

    oci_execute($statement, OCI_DEFAULT);
}

当我以这种方式执行时,该过程会抛出以下错误:

  

PHP警告:oci_execute()[function.oci-execute]:ORA-20100:::期间缺失。::

Period是第四个参数,因此错误消息表明第四个值未设置。

这是更改每次迭代参数值的正确方法吗?

2 个答案:

答案 0 :(得分:1)

我认为您希望将一次绑定到变量(因为它们通过引用传递),更改变量的值并执行多次:

oci_bind_by_name($statement, ":p_fourth", $fourth);
oci_bind_by_name($statement, ":p_fifth", $fifth);

for ($index = 0; $index < 3; $index++) {
    $fourth = $fourthVals[$index];
    $fifth  = $fifthVals[$index];
    oci_execute($statement, OCI_DEFAULT);
}

我希望你的前3个oci_bind_by_name调用可以通过引用错误传递某种只有变量,因为你传递的是文字字符串而不是变量。

答案 1 :(得分:-1)

您的代码对我来说很好。检查PL / SQL实际上如何使用传入的值。

我创建了架构:

drop table mytab;
create table mytab (first varchar2(6), second varchar2(6), third varchar2(6), fourth varchar2(6), fifth varchar2(6));

create or replace procedure DoSomething(p_first varchar2, p_second varchar2, p_third varchar2, p_fourth varchar2, p_fifth varchar2) as
begin
  insert into mytab values (p_first, p_second, p_third, p_fourth, p_fifth);
  commit;
end;
/
show errors

运行PHP代码:

$one = 'A1';
$two = 'B2';
$three = 'C3';

$sql = "BEGIN
    DoSomething(p_first => :p_first,
        p_second => :p_second,
        p_third => :p_third,
        p_fourth => :p_fourth,
        p_fifth => :p_fifth);
    COMMIT;
    END;";

$statement = oci_parse($c, $sql);
oci_bind_by_name($statement, ":p_first", $one);
oci_bind_by_name($statement, ":p_second", $two);
oci_bind_by_name($statement, ":p_third", $three);

$fourthVals = array("a", "b", "c");
$fifthVals = array("x", "y", "z");

for ($index = 0; $index < 3; $index++) {
    oci_bind_by_name($statement, ":p_fourth", $fourthVals[$index]);
    oci_bind_by_name($statement, ":p_fifth", $fifthVals[$index]);

    oci_execute($statement, OCI_DEFAULT);
}

现在的表格包含:

SQL> select * from mytab;

FIRST  SECOND THIRD  FOURTH FIFTH
------ ------ ------ ------ ------
A1     B2     C3     a      x
A1     B2     C3     b      y
A1     B2     C3     c      z