开发者

Oracle Stored Procedure call by PHP

Here the code:

<?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_T开发者_StackOverflowype", $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;

Result is "Procedure Failed". Where am I failing? I've just used a stored procedure call (but with cursors as output) till now and all was fine.

Launching the SP on Oracle works fine so it's a php problem.


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

So, your logic is: if the execute is successful, then the procedure failed?

Just replace with:

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


Magically it works while i was debugging using some echo to print the content of some variables.

I'm sure i've to kill a SysAdmin for these days wasted.


This example work for me:

Reqirements: Pear MDB2 with oracle support

This sample show how to run an oracle package from php using MDB2 framework, using an store procedure with parameters IN, OUT, IN OUT and return the result over php variables.

References:

  • MDB2
  • MDB2 TUTORIAL

Php code:

$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 package definition

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;

The screen shot return:

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

Tested over:

  • Oracle 10g, 11g
  • Linux (Ubuntu server, Amazon EC2) & windows xammp 1.7.4
  • php 5.3.x
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜