我可以从匿名 PL/SQL 块向 PHP 返回值吗?

发布于 2024-09-04 00:15:46 字数 269 浏览 5 评论 0原文

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

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

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

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

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

发布评论

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

评论(2

绮烟 2024-09-11 00:15:46

您可以通过在名称和数据类型声明之间使用关键字OUT 来定义输出参数。 IE:

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

如果没有指定,则默认为IN。如果要将参数同时用作 in 和 out,请使用:

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

以下示例创建一个带有 IN 和 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);
 ?>

参考:

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 ...

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 ...

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

Reference:

丢了幸福的猪 2024-09-11 00:15:46

这是我的决定:

function execute_procedure($procedure_name, array $params = array(), &$return_value = ''){
    $sql = "
    DECLARE
        ERROR_CODE      VARCHAR2(2000);
        ERROR_MSG       VARCHAR2(2000);
        RETURN_VALUE    VARCHAR2(2000);
    BEGIN ";

    $c = $this->get_connection();

    $prms = array();
    foreach($params AS $key => $value) $prms[] = ":$key";
    $prms = implode(", ", $prms);

    $sql .= ":RETURN_VALUE := ".$procedure_name."($prms);";
    $sql .= " END;";


    $s = oci_parse($c, $sql);

    foreach($params AS $key => $value)
    {
        $type = SQLT_CHR;
        if(is_array($value))
        {
            if(!isset($value['value'])) continue;
            if(!empty($value['type'])) $type = $value['type'];
            $value = $value['value'];
        }
        oci_bind_by_name($s, ":$key", $value, -1, $type);
    }

    oci_bind_by_name($s, ":RETURN_VALUE", $return_value, 2000);

    try{
        oci_execute($s);
        if(!empty($ERROR_MSG))
        {
            $data['success'] = FALSE;
            $this->errors = "Ошибка: $ERROR_CODE $ERROR_MSG";
        }
        return TRUE;
    }
    catch(ErrorException $e)
    {
        $this->errors = $e->getMessage();
        return FALSE;
    }
}

示例:

    execute_procedure('My_procedure', array('code' => 5454215), $return_value);
    echo $return_value;

Here my decision:

function execute_procedure($procedure_name, array $params = array(), &$return_value = ''){
    $sql = "
    DECLARE
        ERROR_CODE      VARCHAR2(2000);
        ERROR_MSG       VARCHAR2(2000);
        RETURN_VALUE    VARCHAR2(2000);
    BEGIN ";

    $c = $this->get_connection();

    $prms = array();
    foreach($params AS $key => $value) $prms[] = ":$key";
    $prms = implode(", ", $prms);

    $sql .= ":RETURN_VALUE := ".$procedure_name."($prms);";
    $sql .= " END;";


    $s = oci_parse($c, $sql);

    foreach($params AS $key => $value)
    {
        $type = SQLT_CHR;
        if(is_array($value))
        {
            if(!isset($value['value'])) continue;
            if(!empty($value['type'])) $type = $value['type'];
            $value = $value['value'];
        }
        oci_bind_by_name($s, ":$key", $value, -1, $type);
    }

    oci_bind_by_name($s, ":RETURN_VALUE", $return_value, 2000);

    try{
        oci_execute($s);
        if(!empty($ERROR_MSG))
        {
            $data['success'] = FALSE;
            $this->errors = "Ошибка: $ERROR_CODE $ERROR_MSG";
        }
        return TRUE;
    }
    catch(ErrorException $e)
    {
        $this->errors = $e->getMessage();
        return FALSE;
    }
}

example:

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