macha
macha

Reputation: 7487

Trying to access a select function from Oracle with PHP

Hello I am trying to access a simple function that returns the result of a select query, and when I am accessing it using PHP, it is throwing back a resource(5) at me rather than 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 pretty simple and just retrieves data from a table and returns the few rows that match 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;

Which is fairly simple. Now I am unable to understand why I am getting a resource(5) which is an indication of an error. The actual message I am getting when I var_dump the result is

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

Upvotes: 3

Views: 2961

Answers (2)

Chris Jones
Chris Jones

Reputation: 1

A working example for your definition of selectMe() is:

<?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);

?>

Upvotes: 0

Justin Cave
Justin Cave

Reputation: 231781

I'm not a PHP developer. However, I can steal liberally from the Oracle PHP wiki to guess that it would look something like

$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);

Upvotes: 5

Related Questions