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

PHP: How to call oci_execute multiple times with different parameter values?

我有一个 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 Warning: oci_execute() [function.oci-execute]: ORA-20100: ::Period is missing.::

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

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

我想你想绑定一次变量(因为它们是通过引用传递的),改变变量的值并执行多次:

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 调用出现某种 只能通过引用传递变量 错误,因为您传递的是文字字符串而不是变量.

我觉得你的代码不错。检查您的 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);
}

table 现在包含:

SQL> select * from mytab;

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