//建立一個TEST表
CREATE TABLE TEST (
ID NUMBER(16) NOT NULL,
NAME VARCHAR2(30) NOT NULL,
PRIMARY KEY (ID)
);//插入一條數據
INSERT INTO TEST VALUES (5, 'PHP_BOOK');//建立一個存儲過程
CREATE OR REPLACE PROCEDURE PROC_TEST (
p_id IN OUT NUMBER,
p_name OUT VARCHAR2
) AS
BEGIN
SELECT NAME INTO p_name
FROM TEST
WHERE ID = 5;
END PROC_TEST;
/
--------------------------------------------------------------------------------
PHP代碼:--------------------------------------------------------------------------------
<?php
//建立數據庫連接
$user = "scott"; //數據庫用戶名
$password = "tiger"; //密碼
$conn_str = "tnsname"; //連接串(cstr : Connection_STRing)
$remote = true //是否遠程連接
if ($remote) {
$conn = OCILogon($user, $password, $conn_str);
}
else {
$conn = OCILogon($user, $password);
}//設定綁定
$id = 5; //准備用以綁定的php變量 id
$name = ""; //准備用以綁定的php變量 name/** 調用存儲過程的sql語句(sql_sp : SQL_StoreProcedure)
* 語法:
* BEGIN 存儲過程名([[:]參數]); END;
* 加上冒號表示該參數是一個位置
**/
$sql_sp = "BEGIN PROC_TEST(:id, :name); END;";//Parse
$stmt = OCIParse($conn, $sql_sp);//執行綁定
OCIBindByName($stmt, ":id", $id, 16); //參數說明:綁定php變量$id到位置:id,並設定綁定長度16位
OCIBindByName($stmt, ":name", $name, 30);//Execute
OCIExecute($stmt);//結果
echo "name is : $name<br>";
?>
