我正在尝试执行并从我的程序中获取数据:
以下是我的程序定义方式:
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:在获取或执行之前定义未完成并获取"。
我无法弄清楚我错过了什么。你能帮忙吗?
答案 0 :(得分:9)
与直接从SELECT
语句访问行相比,要在PHP中使用游标需要三个额外的步骤。
oci_new_cursor()
函数在PHP中准备游标资源,然后使用该函数绑定到相应的参数。oci_bind_by_name()
功能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。有一个有趣的例子,请看 Stored Procedures和Reference Cursors 章节!
希望它有所帮助!
答案 1 :(得分:1)
此示例基于Oracle Documentation
// Define MYDB connection string as described in tnsnames.ora
define("MYDB","(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = JXYX.com)(PORT = 1521)))(CONNECT_DATA=(SID=DHSJKS)))");
// Connect to database
$conn = oci_connect("XXXXXX","XXXXXXXX",MYDB);
// Through error if not connected
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Bind the your input and output parameters to PHP variables
$stmt = oci_parse($conn,'BEGIN SP_GET_MY_DATA(:POP, :SEG, :DUR, :VIEW, :PAGE, :OUTPUT_CUR); END;');
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);
// Declare your cursor
$OUTPUT_CUR = oci_new_cursor($conn);
oci_bind_by_name($stmt,":OUTPUT_CUR", $OUTPUT_CUR, -1, OCI_B_CURSOR);
// Execute statement
oci_execute($stmt);
// Execute the cursor
oci_execute($OUTPUT_CUR);
// Fetch results
while ($data = oci_fetch_assoc($OUTPUT_CUR)) {
print_r($data);
}