我可以从匿名PL/SQL块向PHP返回值吗? [英] Can I return values to PHP from an anonymous PL/SQL block?

查看:99
本文介绍了我可以从匿名PL/SQL块向PHP返回值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和OCI8执行匿名的Oracle PL/SQL代码块.我有什么办法可以绑定一个变量并在块完成后获取其输出,就像我以类似方式调用存储过程时一样?

I'm using PHP and OCI8 to execute anonymous Oracle PL/SQL blocks of code. Is there any way for me to bind a variable and get its output upon completion of the block, just as I can when I call stored procedures in a similar way?

$SQL = "declare
something varchar2 := 'I want this returned';
begin
  --How can I return the value of 'something' into a bound PHP variable?
end;";

推荐答案

您可以通过在名称和数据类型声明之间使用关键字OUT来定义out参数. IE浏览器:

You define an out parameter by using the keyword OUT between the name and data type declaration. IE:

CREATE OR REPLACE PROCEDURE blah (OUT_PARAM_EXAMPLE OUT VARCHAR2) IS ...

如果未指定,则默认为IN.如果要同时使用参数作为输入和输出,请使用:

If not specified, IN is the default. If you want to use a parameter as both in and out, use:

CREATE OR REPLACE PROCEDURE blah (INOUT_PARAM_EXAMPLE IN OUT VARCHAR2) IS ...

下面的示例创建一个带有IN和OUT参数的过程.然后执行该过程并打印出结果.

The following example creates a procedure with IN and OUT parameters. The procedure is then executed and the results printed out.

<?php
   // Connect to database...
   $c = oci_connect("hr", "hr_password", "localhost/XE");
   if (!$c) {
      echo "Unable to connect: " . var_dump( oci_error() );
      die();
   }

   // Create database procedure...
   $s = oci_parse($c, "create procedure proc1(p1 IN number, p2 OUT number) as " .
                     "begin" .
                     "  p2 := p1 + 10;" .
                     "end;");
   oci_execute($s, OCI_DEFAULT);

   // Call database procedure...
   $in_var = 10;
   $s = oci_parse($c, "begin proc1(:bind1, :bind2); end;");
   oci_bind_by_name($s, ":bind1", $in_var);
   oci_bind_by_name($s, ":bind2", $out_var, 32); // 32 is the return length
   oci_execute($s, OCI_DEFAULT);
   echo "Procedure returned value: " . $out_var;

   // Logoff from Oracle...
   oci_free_statement($s);
   oci_close($c);
 ?>

参考:

这篇关于我可以从匿名PL/SQL块向PHP返回值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆