尝试使用 PHP 访问 Oracle 的 select 函数

发布于 2024-10-13 05:26:31 字数 950 浏览 5 评论 0原文

您好,我正在尝试访问一个返回选择查询结果的简单函数,当我使用 PHP 访问它时,它向我返回资源(5)而不是结果。

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

selectMe 函数非常简单,只是从表中检索数据并返回与条件匹配的几行。

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;

这相当简单。现在我无法理解为什么我收到资源(5),这表明存在错误。当我 var_dump 结果时我收到的实际消息是

数组(1) { ["SELECTME(:NAME)"]=>; 类型的资源(5)(oci8 语句)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长亭外,古道边 2024-10-20 05:26:31

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

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);
很酷又爱笑 2024-10-20 05:26:31

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

?>

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文