用PHP調(diào)用Oracle存儲過程的方法
更新時間:2008年09月12日 20:42:14 作者:
php程序訪問數(shù)據(jù)庫,完全可以使用存儲過程,有人認為使用存儲過程便于維護。不過仁者見仁,智者見智,在這個問題上,偶認為使用存儲過程意味著必須要dba和開發(fā)人員更緊密配合,如果其中一方更變,則顯然難以維護。
但是使用存儲過程至少有兩個最明顯的優(yōu)點:速度和效率。使用存儲過程的速度顯然更快。在效率上,如果應用一次需要做一系列sql操作,則需要往返于php與oracle,不如把該應用直接放到數(shù)據(jù)庫方以減少往返次數(shù),增加效率。但是在internet應用上,速度是極度重要的,所以很有必要使用存儲過程。偶也是使用php調(diào)用存儲過程不久,做了下面這個列子。
代碼
//建立一個test表
create table test (
id number(16) not null,
name varchar2(30) not null,
primary key (id)
);
//插入一條數(shù)據(jù)
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
//建立數(shù)據(jù)庫連接
$user = "scott"; //數(shù)據(jù)庫用戶名
$password = "tiger"; //密碼
$conn_str = "tnsname"; //連接串(cstr : connection_string)
$remote = true //是否遠程連接
if ($remote) {
$conn = ocilogon($user, $password, $conn_str);
}
else {
$conn = ocilogon($user, $password);
}
//設(shè)定綁定
$id = 5; //準備用以綁定的php變量 id
$name = ""; //準備用以綁定的php變量 name
/** 調(diào)用存儲過程的sql語句(sql_sp : sql_storeprocedure)
* 語法:
* begin 存儲過程名([[:]參數(shù)]); end;
* 加上冒號表示該參數(shù)是一個位置
**/
$sql_sp = "begin proc_test(:id, :name); end;";
//parse
$stmt = ociparse($conn, $sql_sp);
//執(zhí)行綁定
ocibindbyname($stmt, ":id", $id, 16); //參數(shù)說明:綁定php變量$id到位置:id,并設(shè)定綁定長度16位
ocibindbyname($stmt, ":name", $name, 30);
//execute
ociexecute($stmt);
//結(jié)果
echo "name is : $name<br>";
?>
代碼
復制代碼 代碼如下:
//建立一個test表
create table test (
id number(16) not null,
name varchar2(30) not null,
primary key (id)
);
//插入一條數(shù)據(jù)
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
//建立數(shù)據(jù)庫連接
$user = "scott"; //數(shù)據(jù)庫用戶名
$password = "tiger"; //密碼
$conn_str = "tnsname"; //連接串(cstr : connection_string)
$remote = true //是否遠程連接
if ($remote) {
$conn = ocilogon($user, $password, $conn_str);
}
else {
$conn = ocilogon($user, $password);
}
//設(shè)定綁定
$id = 5; //準備用以綁定的php變量 id
$name = ""; //準備用以綁定的php變量 name
/** 調(diào)用存儲過程的sql語句(sql_sp : sql_storeprocedure)
* 語法:
* begin 存儲過程名([[:]參數(shù)]); end;
* 加上冒號表示該參數(shù)是一個位置
**/
$sql_sp = "begin proc_test(:id, :name); end;";
//parse
$stmt = ociparse($conn, $sql_sp);
//執(zhí)行綁定
ocibindbyname($stmt, ":id", $id, 16); //參數(shù)說明:綁定php變量$id到位置:id,并設(shè)定綁定長度16位
ocibindbyname($stmt, ":name", $name, 30);
//execute
ociexecute($stmt);
//結(jié)果
echo "name is : $name<br>";
?>
相關(guān)文章
同臺服務器使用緩存APC效率高于Memcached的演示代碼
之前看到有文章說同臺服務器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個小程序測試了下。2010-02-02php中使用array_filter()函數(shù)過濾數(shù)組實例講解
在本篇文章里小編給大家分享的是一篇關(guān)于php中使用array_filter()函數(shù)過濾數(shù)組實例講解,有興趣的朋友們可以學習下。2021-03-03php判斷文件上傳類型及過濾不安全數(shù)據(jù)的方法
這篇文章主要介紹了php判斷文件上傳類型及過濾不安全數(shù)據(jù)的方法,可實現(xiàn)對$_COOKIE、$_POST、$_GET中不安全字符的過濾功能,非常具有實用價值,需要的朋友可以參考下2014-12-12PHP中輸出轉(zhuǎn)義JavaScript代碼的實現(xiàn)代碼
最近在做天地圖是GIS集成··要輸出HTML到JavaScript里面··涉及到代碼轉(zhuǎn)義什么的比較麻煩··所以寫個PHP的function2011-04-04