php 预编译会话是什么呀 怎么使用 哪道PHP 只能用 存储过程 ??

解决方案 »

  1.   

    如何在php里使用预编译sql
    让动态 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 值。
     
     
    使用范例 
    这个范例是 [email protected] 所提出的,它加入三笔资料到 emp 资料表中,并使用 ROWID 来更新资料。<?php
    $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);
    ?>