首页 > 解决方案 > 使用具有自定义数组作为参数的php执行oracle函数

问题描述

我有一个 oracle 函数,它有一个数组作为参数,如下所示:

addGroups($empGroupId, $employees);

它添加了员工组,但此数组参数在 oracle 数据库中定义为自定义类型,如下所示:

CREATE OR REPLACE TYPE EMPLOYEES."ARR_VAR2"  is table of varchar2(32000);

我试图在 php 中调用这个函数并传递这个数组,如下所示:

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

//recieving parameters
$empGroupId= $request->ID;
$employees= $request->EMPLOYEES;

//establish connection
$conn = oci_connect("EMPLOYEES", "****", "ip/orcl");

//test connection
if ($conn)
{
  if($empGroupId!=NULL && $employees!=NULL)
  {
      $stmt =  "begin :resu :=addGroups(:empGroupId,:employees, :result ); end;";
      $stid=oci_parse($conn,$stmt);

      oci_bind_by_name($stid, ":empGroupId", $empGroupId);
      oci_bind_array_by_name($stid, ":employees", $employees, 250, 250, SQLT_VCS );
      oci_bind_by_name($stid, ":result", $result, 40);
      oci_bind_by_name($stid, ":resu", $resu, 100);

      oci_execute($stid);
      oci_free_statement($stid);

      $result1['output'] = array("result" => $result);
      return $result1;
else
{
//connection failed
echo 500;
}

但它返回此错误:

ORA-01458: 可变字符串内的长度无效

我还尝试了 oci new collection 如下:

 $stmt =  "begin :resu :=addGroups(:empGroupId,:employees, :result ); end;";
      $stid=oci_parse($conn,$stmt);

      oci_bind_by_name($stid, ":empGroupId", $empGroupId);
      $IDs= oci_new_collection($conn, 'ARR_VAR', 'EMPLOYEES');
     foreach($employees as $emp)
     {
       $IDs->append($emp);
     }
     oci_bind_by_name($stid, ":employees", $IDs, -1, SQLT_CHR );
      oci_bind_by_name($stid, ":result", $result, 40);
      oci_bind_by_name($stid, ":resu", $resu, 100);

      oci_execute($stid);
      oci_free_statement($stid);

      $result1['output'] = array("result" => $result);
      return $result1;
else
{
//connection failed
echo 500;
}

它给了我这个错误:

警告:oci_new_collection():ORA-22318:输入类型不是数组类型

致命错误:未捕获的错误:在布尔值上调用成员函数 append()

有什么办法可以解决这个问题

提前致谢

标签: phparraysoraclefunctionparameters

解决方案


非常感谢克里斯托弗,这对我的问题解决非常有帮助,而且我似乎应该在创建新集合后放置 pl/sql 语句,如下所示:

$conn = $this->conn;
    $IDs = oci_new_collection($conn, 'ARR_VAR');
    for ($i = 0; $i < count($employees ); ++$i)
     {
       $IDs->append($employees[$i]);
     }
    $stmt =  "begin :resu :=addGroups(:empGroupId,:employees, :result ); end;";

    $stid=oci_parse($conn,$stmt);
    oci_bind_by_name($stid, ":empGroupId", $empGroupId);
    oci_bind_by_name($stid, ":employees", $IDs, -1, OCI_B_NTY );
    oci_bind_by_name($stid, ":result", $result, 40);
    oci_bind_by_name($stid, ":resu", $resu, 100);
    oci_execute($stid);
    oci_free_statement($stid);
    $result1['output'] = array("result" => $result);
    return $result1;

推荐阅读