gpt4 book ai didi

php - 从 PHP 调用 Oracle 存储过程

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:00:27 24 4
gpt4 key购买 nike

我正在尝试执行我的程序并从中获取数据:

这里是我的过程定义的方式:

create or replace PROCEDURE SP_GET_MY_DATA(
IN_POP VARCHAR2,
IN_SEG VARCHAR2,
IN_DUR VARCHAR2,
IN_VIEW INTEGER,
IN_PAGE INTEGER,
VIEW_DATA_CUR OUT SYS_REFCURSOR) AS ...

这是我的 PHP 代码,用于执行过程并从过程中获取数据:

$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = JXYX.com)(PORT = 1521)))(CONNECT_DATA=(SID=DHSJKS)))";
$conn = ocilogon("XXXXXX","XXXXXXXX",$db);

$sql = 'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;';

$stmt = oci_parse($conn,$sql);
oci_bind_by_name($stmt,':POP',$pop);
oci_bind_by_name($stmt,':SEG',$seg);
oci_bind_by_name($stmt,':DUR',$dur);
oci_bind_by_name($stmt,':VIEW',$view);
oci_bind_by_name($stmt,':PAGE',$page);
$OUTPUT_CUR = oci_new_cursor($conn);
oci_bind_by_name($stmt,":OUTPUT_CUR", $OUTPUT_CUR, -1, OCI_B_CURSOR);
oci_execute($stmt, OCI_DEFAULT);

while ($data = oci_fetch_assoc($OUTPUT_CUR)) {
print_r($data);
}

但是在这样做时我得到了这个错误:

oci_fetch_assoc(): ORA-24374: define not done before fetch or execute and fetch".

我无法弄清楚我错过了什么。你能帮忙吗?

最佳答案

与直接从 SELECT 语句访问行相比,要在 PHP 中使用游标需要三个额外的步骤。

  • 第一步是使用 oci_new_cursor() 函数在 PHP 中准备游标资源,然后使用该函数绑定(bind)到适当的参数。
  • 第二步是在oci_bind_by_name()函数上添加一个参数
  • 第三步,在您执行了通常的 SQL 语句之后,是在游标资源上调用 oci_execute()

代码:

//Connection does not change
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = JXYX.com)(PORT = 1521)))(CONNECT_DATA=(SID=DHSJKS)))";
$conn = ocilogon("XXXXXX","XXXXXXXX",$db);

//Request does not change
$sql = 'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;';

//Statement does not change
$stmt = oci_parse($conn,$sql);
oci_bind_by_name($stmt,':POP',$pop);
oci_bind_by_name($stmt,':SEG',$seg);
oci_bind_by_name($stmt,':DUR',$dur);
oci_bind_by_name($stmt,':VIEW',$view);
oci_bind_by_name($stmt,':PAGE',$page);

//But BEFORE statement, Create your cursor
$cursor = oci_new_cursor($conn)

// On your code add the latest parameter to bind the cursor resource to the Oracle argument
oci_bind_by_name($stmt,":OUTPUT_CUR", $cursor,-1,OCI_B_CURSOR);

// Execute the statement as in your first try
oci_execute($stmt);

// and now, execute the cursor
oci_execute($cursor);

// Use OCIFetchinto in the same way as you would with SELECT
while ($data = oci_fetch_assoc($cursor, OCI_RETURN_LOBS )) {
print_r($data);
}

我对 Oracle(和英语)不是很流利,所以你应该阅读 this tutorial .有一个有趣的例子,请看存储过程和引用游标一章!

希望对您有所帮助!

关于php - 从 PHP 调用 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com