php - 使用具有自定义数组作为参数的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()
有什么办法可以解决这个问题
提前致谢
解决方案
非常感谢克里斯托弗,这对我的问题解决非常有帮助,而且我似乎应该在创建新集合后放置 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;
推荐阅读
- codeigniter - 在 Codeigniter 中更新数据库多个上传文件的数据
- reactjs - 我们可以在 React 中用 className 或 id 替换 ref 吗?
- python - 无法正确配置 conda 环境:env 路径错误
- javascript - 如何制作“谷歌表格”类似的表格?
- web-scraping - 使用 Google 表格进行网页抓取。importxml函数xpath修正
- python - Django Forms(带有酥脆的表格)-我是否过于复杂了?有没有更简单的方法?
- javascript - 来自后台脚本的 sendResponse 中的数组数据丢失
- r - Knitr/Kable:Html 表格宽度
- javascript - 在输入框而不是 JavaScript 中的 alert() 旁边显示错误
- reactjs - 使用 Next.js useRouter 拉标题道具不起作用