[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 編程技術 > Php > 正文
PHP4手冊:函數庫及函數(三十七) Oracle 8 資料庫函式庫
http://netleader.126.com 星空浪子 (2001-04-18 15:39:13)

--------------------------------------------------------------------------------
 Oracle 8 資料庫函式庫 
--------------------------------------------------------------------------------
 


本函式庫共有 15 個函式
由本函式庫都是呼叫 Oracle8 Call-Interface (OCI8) 來存取 Oracle 資料庫,因此在裝設 Oracle 8 Client 的 Web 伺服器上,可用本函式庫存取 Oracle 7.x 或 8.x 二種版本的資料庫伺服器。  


OCIDefineByName: 讓 SELECT 指令可使用 PHP 變數。 
OCIBindByName: 讓動態 SQL 可使用 PHP 變數。 
OCILogon: 開啟與 Oracle 的連結。 
OCILogOff: 關閉與 Oracle 的連結。 
OCIExecute: 執行 Oracle 的指令區段。 
OCICommit: 將 Oracle 的交易處理付諸實行。 
OCIRollback: 撤消當前交易。 
OCINumRows: 取得受影響欄位的數目。 
OCIResult: 從目前列 (row) 的資料取得一欄 (column)。 
OCIFetch: 取得傳回資料的一列 (row)。 
OCIFetchInto: 取回 Oracle 資料放入陣列。 
OCIColumnIsNULL: 測試傳回行是否為空的。 
OCIColumnSize: 取得欄位型態的大小。 
OCINewDescriptor: 初始新的 LOB/FILE 描述。 
OCIParse: 分析 SQL 語法。 


--------------------------------------------------------------------------------
 函式:OCIDefineByName() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIDefineByName
讓 SELECT 指令可使用 PHP 變數。

語法: boolean OCIDefineByName(int stmt, string ColumnName, mixed &variable, int [type]);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式用來定義指定的 PHP 變數,使其能供 SQL 指令中的 SELECT 指令使用。在大小寫的問題上要注意一下,因為 Oracle 資料庫中的欄位名稱其實都是大寫的名字。參數 stmt 是經過 Oracle 解析 (OCIParse) 的字串指標。參數 ColumnName 是 Oracle 資料表上的欄位名稱。參數 variable 前面一定要加 & 符號,表 PHP 變數位址。參數 type 通常省略。值得注意的是欲使用 Oracle 8 中特有的新資料型態 LOB/ROWID/BFILE 等時,需要先執行 OCINewDescriptor() 函式。執行本函式成功則傳回 true 值。


 
 
使用范例 


這個范例是 thies@digicol.de 所提出的
$conn = OCILogon("scott","tiger");
$stmt = OCIParse($conn,"select empno, ename from emp");
/* 使用 OCIDefineByName 要在執行 OCIExecute 前 */
OCIDefineByName($stmt,"EMPNO",&$empno);
OCIDefineByName($stmt,"ENAME",&$ename);
OCIExecute($stmt);
while (OCIFetch($stmt)) {
  echo "empno:".$empno."\n";
  echo "ename:".$ename."\n";
}
OCIFreeStatement($stmt);
OCILogoff($conn);
?> 

--------------------------------------------------------------------------------
 函式:OCIBindByName() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIBindByName
讓動態 SQL 可使用 PHP 變數。

語法: boolean OCIBindByName(int stmt, string ph_name, mixed &variable, int length, int [type]);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式用來定義指定的 PHP 變數,使其能供動態的 SQL 指令 (Oracle Placeholder) 使用。在大小寫的問題上要注意一下,因為 Oracle 資料庫中的欄位名稱其實都是大寫的名字。參數 stmt 是經過 Oracle 解析 (OCIParse) 的字串指標。參數 ph_name 即為欲供動態 SQL 指令所使用的變數。參數 variable 前面一定要加 & 符號,表 PHP 變數位址。參數 length 為資料的長度,若設為 -1 則使用指定的 variable 資料最大值。參數 type 可省略,其值有 OCI_B_FILE (二進位檔)、OCI_B_CFILE (文字檔)、OCI_B_CLOB (文字 LOB)、OCI_B_BLOB (位元 LOB) 及 OCI_B_ROWID (ROWID) 等數種。值得注意的是欲使用 Oracle 8 中特有的新資料型態 LOB/ROWID/BFILE 等時,需要先執行 OCINewDescriptor() 函式,同時必須要將 length 參數設成 -1。執行本函式成功則傳回 true 值。


 
 
使用范例 


這個范例是 thies@digicol.de 所提出的,它加入三筆資料到 emp 資料表中,並使用 ROWID 來更新資料。

$conn = OCILogon("scott", "tiger");
$stmt = OCIParse($conn,"insert into emp (empno, ename) "."values (:empno,:ename) "."returning ROWID into :rid");
$data = array(1111 => "Larry", 2222 => "Bill", 3333 => "Jim");
$rowid = OCINewDescriptor($conn, OCI_D_ROWID);
OCIBindByName($stmt, ":empno", &$empno, 32);
OCIBindByName($stmt, ":ename", &$ename, 32);
OCIBindByName($stmt, ":rid", &$rowid, -1, OCI_B_ROWID);
$update = OCIParse($conn, "update emp set sal = :sal where ROWID = :rid");
OCIBindByName($update, ":rid", &$rowid, -1, OCI_B_ROWID);
OCIBindByName($update, ":sal", &$sal, 32);
$sal = 10000;
while (list($empno, $ename) = each($data)) {
  OCIExecute($stmt);
  OCIExecute($update);
} 
$rowid->free();
OCIFreeStatement($update);
OCIFreeStatement($stmt);
$stmt = OCIParse($conn, "select * from emp where empno in (1111,2222,3333)");
OCIExecute($stmt);
while (OCIFetchInto($stmt, &$arr, OCI_ASSOC)) {
  var_dump($arr);
}
OCIFreeStatement($stmt);
/* 刪除剛加在 emp 資料表中的三筆資料 */
$stmt = OCIParse($conn, "delete from emp where empno in (1111,2222,3333)");
OCIExecute($stmt);
OCIFreeStatement($stmt);
OCILogoff($conn);
?> 


--------------------------------------------------------------------------------
 函式:OCILogon() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCILogon
開啟與 Oracle 的連結。

語法: int OCILogon(string username, string password, string [OCACLE_SID]);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式使 PHP 與 Oracle 建立連結。參數 username 與 password 分別為連線的帳號及密碼。參數 OCACLE_SID 為資料庫名稱,可省略。傳回值為連線的代碼。

 

--------------------------------------------------------------------------------
 函式:OCILogOff() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCILogOff
關閉與 Oracle 的連結。

語法: boolean OCILogOff(int connection);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式使 PHP 與 Oracle 的連結結束。參數 connection 為連上 Oracle 的連線代碼。傳回值 true 表示成功,false 表示發生錯誤。

 

--------------------------------------------------------------------------------
 函式:OCIExecute() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIExecute
執行 Oracle 的指令區段。

語法: boolean OCIExecute(int statement, int [mode]);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式用來執行指定的 Oracle 指令區段,執行前必須先由 OCIParse() 剖析過該區段的 SQL 語法。參數 statement 為剖析過的代碼。參數 mode 可省略,其內定值為 OCI_COMMIT_ON_SUCCESS。傳回值 true 表示成功,false 表示發生錯誤。

 

--------------------------------------------------------------------------------
 函式:OCICommit() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCICommit
將 Oracle 的交易處理付諸實行。

語法: boolean OCICommit(int connection);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式會將最近一次 commit/rollback 的交易 (transaction) 做永久性的修改。參數 connection 為連上 Oracle 的連線代碼。傳回值 true 表示成功,false 表示發生錯誤。

 

--------------------------------------------------------------------------------
 函式:OCIRollback() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIRollback
撤消當前交易。

語法: boolean OCIRollback(int connection);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式取消 Oracle 交易處理 (transaction) 對資料庫所做的修改。參數 connection 為連上 Oracle 的連線代碼。若成功則傳回 true,反之傳回 false。

 

--------------------------------------------------------------------------------
 函式:OCINumRows() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCINumRows
取得受影響欄位的數目。

語法: int OCINumRows(int statement);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式傳回受 UPDATE 等指令影響的欄位 (column) 數目,若使用 SELECT 等 SQL 指令則不會有影響。參數 statement 為剖析過的代碼。

 

--------------------------------------------------------------------------------
 函式:OCIResult() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIResult
從目前列 (row) 的資料取得一欄 (column)。

語法: string OCIResult(int statement, mixed column);

傳回值: 字串

函式種類: 資料庫功能


 
 
內容說明 


本函式傳回傳回一欄資料。參數 statement 為剖析過的代碼。參數 column 為欄位名。若使用新的資料形態 (ROWIDs、LOBs 與 FILEs) 傳回亦均
 

--------------------------------------------------------------------------------
 函式:OCIFetch() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIFetch
取得傳回資料的一列 (row)。

語法: int OCIFetch(int statement);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式用來取得一列非空的資料。參數 statement 為剖析過的代碼。傳回值 true 表示成功取回一列,false 表示本列是空的或發生其它錯誤。

 

--------------------------------------------------------------------------------
 函式:OCIFetchInto() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIFetchInto
取回 Oracle 資料放入陣列。

語法: int OCIFetchInto(array &result, int [mode]);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式將對 Oracle 取回的資料放入陣列 result 中。傳回每列的欄位數目,若失敗則傳回 false。參數 mode 可省略,內定值為 OCI_NUM,其它還有 OCI_ASSOC、OCI_RETURN_NULLS 及 OCI_RETURN_LOBS 等。
 

--------------------------------------------------------------------------------
 函式:OCIColumnIsNULL() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIColumnIsNULL
測試傳回行是否為空的。

語法: boolean OCIColumnIsNULL(int stmt, mixed column);

傳回值: 布林值

函式種類: 資料庫功能


 
 
內容說明 


本函式用來測試傳回的行 (column) 是否為空值 (NULL)。傳回 true 表示為空值。

 

--------------------------------------------------------------------------------
 函式:OCIColumnSize() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIColumnSize
取得欄位型態的大小。

語法: int OCIColumnSize(int stmt, mixed column);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式可以取得欄位 (column) 型態 (type) 的大小。

 

--------------------------------------------------------------------------------
 函式:OCINewDescriptor() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCINewDescriptor
初始新的 LOB/FILE 描述。

語法: string OCINewDescriptor(int connection , int [type]);

傳回值: 字串

函式種類: 資料庫功能


 
 
內容說明 


本函式用來初始化新的 LOB/FILE 描述值。

 

--------------------------------------------------------------------------------
 函式:OCIParse() 
--------------------------------------------------------------------------------
 


Oracle 8 資料庫函式庫


OCIParse
分析 SQL 語法。

語法: int OCIParse(int connection, string query);

傳回值: 整數

函式種類: 資料庫功能


 
 
內容說明 


本函式可用來分析 SQL 語法或是 PL/SQL 區段是否有錯誤。參數 connection 為連線代碼。參數 query 為 SQL 指令字串。 (http://www.fanqiang.com)
    進入【UNIX論壇

相關文章
PHP4手冊:函數庫及函數(四十六) SNMP 網管函式庫 (2001-04-18 16:56:55)
PHP4手冊:函數庫及函數(四十五) Solid 資料庫連結函式庫 (2001-04-18 16:54:16)
PHP4手冊:函數庫及函數(四十四) 信號與共享記憶體函式庫 (2001-04-18 16:49:09)
PHP4手冊:函數庫及函數(四十三) 常規表示法函式庫 (2001-04-18 16:40:33)
PHP4手冊:函數庫及函數(四十二) URL 處理函式庫 (2001-04-18 16:37:13)
PHP4手冊:函數庫及函數(四十一) PostgreSQL 資料庫函式庫 (2001-04-18 15:58:12)
PHP4手冊:函數庫及函數(四十) PDF 格式檔案函式庫 - 2 (2001-04-18 15:53:53)
PHP4手冊:函數庫及函數(四十) PDF 格式檔案函式庫 - 1 (2001-04-18 15:53:35)
PHP4手冊:函數庫及函數(三十九) Perl 相容語法函式庫 (2001-04-18 15:45:08)
PHP4手冊:函數庫及函數(三十八) Oracle 資料庫函式庫 (2001-04-18 15:43:09)

===更多相關===
 

★  樊強制作 歡迎分享  ★