oci_emp_cur1 |
Objectives:
- Creating stored procedure, with refcursor as an OUT
parameter
- OCI_B_CURSOR and oci_new_cursor function
- Calling stored procedure with "call "
$stmt = oci_parse($conn, "call
emp_proc1(:rc)");
$refcur = oci_new_cursor($conn);
oci_bind_by_name($stmt, ':rc', $refcur, -1, OCI_B_CURSOR);
|
REF CURSORS let you return a
set of query results to PHP client |
Create a procedure using SQL developer or DDL TooL. Note EMP_CUR1
with white icon, was the name of the cursor defined within the procedure
EMP_PROC1
SCRIPT;
---- stored procedure and cursor ---
create or replace
procedure emp_proc1
(emp_cur1 OUT SYS_REFCURSOR)AS
begin
open emp_cur1 for select *
from emp where job ='SALESMAN';
end;
------
|
PHP scripting fetching cursor as a parameter
<?php
echo "Oracle stored procedure and SYS_REFCURSOR <br/>";
//
$conn = oci_connect('scott', 'Son', 'localhost/orcl.gateway.2wire.net');
// Excute the call to the PL/SQL stored procedure
$sql = "select * from emp";
$stmt1 =oci_parse($conn, $sql);
oci_execute($stmt1);
// HEADER TABLE
echo "<table border=\"1\">";
echo "<tr>";
$ncols = oci_num_fields($stmt1);
for ($i = 1; $i <= $ncols; $i++) {
$column_name = oci_field_name($stmt1, $i);
echo "<td class='td1'>$column_name</td>";
}
//
echo "</tr></table>\n";
oci_free_statement($stmt1);
//
$stmt = oci_parse($conn,
"call emp_proc1(:rc)");
$refcur = oci_new_cursor($conn);
oci_bind_by_name($stmt, ':rc', $refcur, -1, OCI_B_CURSOR);
oci_execute($stmt);
// Execute and fetch from the cursor
oci_execute($refcur);
// treat the returned cursor as an OCI8 statement resource
//report
$nrows ="";
echo "<table border='1'>\n";
while($row = oci_fetch_array($refcur, OCI_ASSOC)) {
echo "<tr>";$nrows++;
foreach ($row as $item) {
echo "<td class='td2'>". htmlentities($item) . "</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
//
echo " no of rows : ". $nrows;
oci_free_statement($stmt);
oci_close($conn);
echo "<br/> Oracle db conection closed";
//
?>
|
Runtime display:
|
|
Notes:
In PHP the oci_new_cursor() function returns a REF CURSOR resource.
This is bound to :rc in the call to emp_cur1(). The bind size of -1
means “ignore the size passed”. It is used because |
|