【问题标题】:Oracle Stored Procedure call by PHP通过 PHP 调用 Oracle 存储过程
【发布时间】:2011-03-13 19:17:13
【问题描述】:

代码如下:

<?php
include_once 'config.php';

// Connect to database
$conn = oci_connect($dbuser, $dbpasswd, $dbhost."/".$dbname);
if (!$conn) {
    exit ("Connection failed.");
}

$id   = isset($_GET['id']) ? (int)$_GET['id'] : false;
$type = isset($_GET['type']) ? strtoupper($_GET['type']) : "BLOG";

$stmt = oci_parse($conn, 
    "begin 
    PKG_LIKE.get_LikeId(
    :I_N_Id,
    :I_S_Type,
    :O_N_grade,
    :O_N_exitFlag,
    :O_S_exitMsg);
    end;");

oci_bind_by_name($stmt, "I_N_Id", $id);
oci_bind_by_name($stmt, "I_S_Type", $type);
oci_bind_by_name($stmt, "O_N_grade", $total);
oci_bind_by_name($stmt, "O_N_exitFlag", $flag);
oci_bind_by_name($stmt, "O_S_exitMsg", $message);

if (!oci_execute($stmt)) {
    exit("Procedure Failed.");
}

if ($message == 'OK') {
    $response = array('likeit' => $total);
    $toReturn = "var response=".json_encode($response)."; showTotalLikeit(response);";
} else {
    $response = array('likeit' => 'NaN', 'exitFlag' => $flag, 'exitMsg' => $message);
    $toReturn = "var response=".json_encode($response)."; showTotalLikeit(response);";
}

print $toReturn;

结果是“程序失败”。我哪里失败了? 到目前为止,我刚刚使用了一个存储过程调用(但使用游标作为输出),一切都很好。

在 Oracle 上启动 SP 工作正常,所以这是一个 php 问题。

【问题讨论】:

  • 如果我是你,我会尽可能查看 PHP 的 PDO 扩展名 (php.net/manual/en/book.pdo.php),因为它支持 Oracle 数据库并调用存储过程。
  • 我肯定会看它,但没有理由代码不起作用?完全相同的“模板”代码与同一数据库上的其他 SP 一起使用并且工作正常:|

标签: php oracle stored-procedures


【解决方案1】:
if (oci_execute($stmt)) {
    exit("Procedure Failed.");
}

那么,你的逻辑是:如果执行成功,那么程序就失败了?

只需替换为:

if (!oci_execute($stmt)) {
   exit("Procedure Failed.");
}

【讨论】:

  • ...我要睡觉了。无论如何,现在页面打印正确“程序失败”......那么,出了什么问题? ^^(要编辑问题)
  • 我无法复制,因为我这里没有 Oracle,但你能试试看 oci_bind_by_name 返回 true 还是 false?
  • 检查:全部正确。和oci_parse,返回资源id。
【解决方案2】:

当我使用一些 echo 来打印一些变量的内容时,它神奇地起作用了。

我确定我必须杀死一个系统管理员来浪费这些天。

【讨论】:

    【解决方案3】:

    这个例子对我有用:

    要求: 支持 oracle 的 Pear MDB2

    这个示例展示了如何使用 MDB2 框架从 php 运行 oracle 包,使用带有参数 IN、OUT、IN OUT 的存储过程并通过 php 变量返回结果。

    参考资料:

    php代码:

    $in = "IN";
    $out = "OUT";
    $io = "INOUT";
    // to show vars after
    var_dump("{$in}::{$out}::{$io}");
    //
    $sql = "BEGIN PKG_TEST.MyProcedure(:iparm, :oparm, :ioparm); END;";
    $sth = $mdb2->prepare($sql);
    //
    $sth->bindParam('iparm', $in, 'text', 20);
    $sth->bindParam('oparm', $out, 'text', 20 );
    $sth->bindParam('ioparm', $io, 'text', 20);
    //
    $res = $sth->execute();
    //
    if (PEAR::isError($res)) {
        var_dump($res->userinfo);
    }else{
        $sth->free();
    }
    // to show vars before
    var_dump("{$in}::{$out}::{$io}");
    

    Oracle 包定义

    CREATE OR REPLACE PACKAGE PKG_TEST AS
    
      PROCEDURE MyProcedure(P1 IN VARCHAR2, P2 OUT VARCHAR2,  P3 IN OUT VARCHAR2);
    
    END PKG_TEST;
    
    CREATE OR REPLACE PACKAGE BODY PKG_TEST IS
    
      PROCEDURE MyProcedure(P1 IN VARCHAR2, P2 OUT VARCHAR2, P3 IN OUT VARCHAR2)
      IS
      BEGIN
        P2 := P1 || '---- OUT ----';
        P3 := P1 || '---- IN OUT ----';
      END MyProcedure;
    
    END PKG_TEST;
    

    截图返回:

    string(14) "IN::OUT::INOUT"
    string(39) "IN::IN---- OUT ----::IN---- IN OUT ----"
    

    测试结束:

    • Oracle 10g、11g
    • Linux(Ubuntu 服务器、Amazon EC2)和 windows xammp 1.7.4
    • php 5.3.x

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 2020-12-12
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多