Attempting to access select function from Oracle with PHP

Hello, I'm trying to access a simple function that returns the result of a select query, and when I access it using PHP, it returns to me (5) the resource (not the result).

$connect = oci_connect('tiger','scott','host/user');
if(!$connect){
$e = oci_error();
trigger_error(htmlentities($e['message'],ENT_QUOTES),E_USER_ERROR);
}


$qu = oci_parse($connect, 'select selectMe(:name) from dual');
$name = (string)'test1';
oci_bind_by_name($qu,":name",$name);

oci_execute($qu);

$row = oci_fetch_assoc($qu);
var_dump($row);

The selectMe function is quite simple and simply retrieves data from the table and returns a few rows matching the condition.

CREATE OR REPLACE FUNCTION selectMe( temp_name varchar2(100) ) 
  return SYS_REFCURSOR is  my_ret SYS_REFCURSOR;
BEGIN
 open my_ret
   FOR select myTab_ID, myTab_NAME, myTab_AGE, myTab_SCORE 
         from myTab 
        where trim(myTab_name) = temp_name;
   RETURN my_ret;   
END;

It is pretty simple. Now I can not understand why I get the resource (5), which is a sign of error. The actual message that I get when I var_dump the result

array (1) {["SELECTME (: NAME)"] => resource (5) of type (oci8 instruction)

+3
source share
2 answers

PHP. , Oracle PHP wiki, ,

$conn = oci_connect('myusername', 'mypassword', 'mydb');

$stid = oci_parse($conn, "begin :rc := selectMe(:name); end;"); 
$refcur = oci_new_cursor($conn);
oci_bind_by_name($stid, ':rc', $refcur, -1, OCI_B_CURSOR);
oci_bind_by_name($stid, ':name', 'test1');
oci_execute($stid);

oci_execute($refcur);
oci_fetch_all($refcur, $res);
var_dump($res);

oci_free_statement($stid);
oci_close($conn);
+4

selectMe():

<?php

$conn = oci_connect('cj', 'cj', 'localhost/xe');

$stid = oci_parse($conn, "select selectMe(:name) as rc from dual"); 
$name = (string)'test1';
oci_bind_by_name($stid, ":name", $name);
oci_execute($stid);
$r = oci_fetch_array($stid);

$refcur = $r['RC'];  // the returned record is the REF CURSOR which can be treated like a PHP statement resource
oci_execute($refcur);
oci_fetch_all($refcur, $res);
var_dump($res);

?>
0

Source: https://habr.com/ru/post/1786747/


All Articles