3

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)

4
  • SYS_REFCURSOR is not an object you return from a SELECT statement. Commented Jan 20, 2011 at 18:39
  • Well the function is working, I tested it using sqlplus and sqldeveloper. I also am not getting any errors, I only receive that resource message on a var_dump of the resource handler. Commented Jan 20, 2011 at 18:42
  • How are you testing the function? I doubt using the code you posted Commented Jan 20, 2011 at 18:45
  • select selectMe('some name') from dual, and I receive the correct output in the script output. Commented Jan 20, 2011 at 18:46

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

you are a real genius!! Thank you for saving me Sir!! If you do have a moment, could you explain a little on why I would need to get that oci_new_cursor?
0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.