【问题标题】:Execute oracle function with php having a custom array as a parameter使用具有自定义数组作为参数的php执行oracle函数
【发布时间】:2020-06-23 04:43:14
【问题描述】:

我有一个 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()

有没有办法解决这个问题

提前致谢

【问题讨论】:

标签: php arrays oracle function parameters


【解决方案1】:

非常感谢 christopher,这对我的问题解决非常有帮助,而且我似乎应该在创建新集合后放入 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;

【讨论】:

  • 还可以查看oci_bind_array_name() 的用法。可能会有性能优势——即使您必须编写一些额外的 PL/SQL 包装器代码。
猜你喜欢
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
相关资源
最近更新 更多