OCIBindByName

OCIBindByName -- Oracle プレースホルダーに PHP 変数をバインドする

説明

int OCIBindByName(int stmt, string ph_name, mixed &variable, intlength, int [type]);

OCIBindByName は、PHP 変数 variable を Oracle プレースホルダー ph_name にバインドします。 実行時に入力用、出力用に使用されるによらず、必要な記憶領域が確保されます。 length パラメータは、バインド時の最大長を設定します。 length を -1 に設定した場合、 OCIBindByNamevariable の現在の長さを 最大長として設定します。

抽象データ型 (LOB/ROWID/BFILE) をバインドする必要がある場合、 まず OCINewDescriptor 関数を使用してこれを確保する 必要があります。 length は抽象データ型用には使用されず、-1 を設定する必要があります。 type 変数は使用したいディスクリプタの種類を Oracle に伝えます。 使用可能な値は次のようになります。: OCI_B_FILE (Binary-File), OCI_B_CFILE (Character-File), OCI_B_CLOB (Character-LOB), OCI_B_BLOB (Binary-LOB) ,OCI_B_ROWID (ROWID)

例 1. OCIDefineByName


<?php
/* OCIBindByPos の例 thies@digicol.de (980221)

  3 レコードを 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 から "junk" を削除する.... */
$stmt = OCIParse($conn,"delete from emp where empno in (1111,2222,3333)");
OCIExecute($stmt);
OCIFreeStatement($stmt);

OCILogoff($conn);
?>