程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 操作Oracle的php類

操作Oracle的php類

編輯:關於PHP編程

<?
//【警告】:未經許可請勿隨便修改
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//   
// 【文件名】:                c_ora_db.inc
// 【作  用】:                Oracle公用函數類
// 【作  者】:                天灰
//  
// 【最後修改日期】:        2001/05/11[cxx]      
// 【變量定義規則】:‘C_'=字符型,‘I_'=整型,‘N_'=數字型,‘L_'=布爾型,‘A_'=數組型
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//    ※db_logon()                    開啟數據庫連接     
//    ※db_query()                    通用select             
//    ※db_change()                    數據庫改變的通用函數(insert,delete,update)
//    ※db_insert()                    insert,直接調用db_change()
//    ※db_delete()                    delete,直接調用db_change()
//    ※db_update()                    update,直接調用db_change()                 
//    ※db_commit()                    事務遞交
//    ※db_rollback()                    事務回退
//    ※db_logoff()                    斷開數據庫連接
//------------------------------------------------------------------------------------------


Class c_ora_db
{

     
//------------------------------------------------------------------------------------------
//        變量定義
//------------------------------------------------------------------------------------------
    var $C_user          = "";              //數據庫用戶名
    var $C_passwd        = "";            //數據庫口令
    var $C_db            = "";                    //數據庫名
    var $I_linkID        = 0;                          //連線句柄
    var $I_stmtID        = 0;                          //查詢句柄
    var $color             ="";                    //全局顏色
//------------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------------
//        函數名:db_logon()
//        作  用:開啟數據庫連接
//        參  數:無
//        返回值:連線句柄(整型)
//        備  注:無
//------------------------------------------------------------------------------------------
    function  db_logon()    
    {    
        $this->I_linkID =  @OCILogon($this->C_user,$this->C_passwd,$this->C_db);
        if ($this->I_linkID == 0){AlertExit('數據庫鏈接失敗,請與DBA聯系!');}
        return  $this->I_linkID;    
    }
//------------------------------------------------------------------------------------------

       
//------------------------------------------------------------------------------------------
//        函數名:db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
//        作  用:select
//        參  數:$C_sql                    sql語句
//                $A_define                需綁定的字段。數組型         
//                $I_start                開始取記錄 -1則取出查詢的所有記錄
//                $I_end                    結束取紀錄
//        返回值:二維數組($A_rs)
//        備  注:通過數字0,1,2....可訪問對應字段的值; 或通過查詢字段名也可訪問對應字段的值
//                如通過$A_rs[0][0]或$A_rs[0]['NAME']或$A_rs[0]['name']都可訪問首條記錄NAME字段
//                $I_start,$I_end是配合分頁使用的參數。
//------------------------------------------------------------------------------------------
    function  db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
    {    
       if (!$C_sql){AlertExit("參數不全!");}//檢查參數

       //連接檢測
       if ($this->I_linkID == 0){AlertExit('數據庫鏈接失敗,請與DBA聯系!');}

       //格式檢測
       $this -> I_stmtID = OCIParse($this -> I_linkID,$C_sql);     
       if (!$this -> I_stmtID){AlertExit(' sql格式出錯!請與程序員聯系');}

       //如果沒指定綁定的字段,則從SQL語句中去取
       if($A_define=="")
       {
            $A_Cur = explode("select",$C_sql);
            $A_Cur = explode("from",$A_Cur[1]);
            $A_define = explode(",",$A_Cur[0]);
       }

           //綁定數據庫表字段
        if(gettype($A_define) == "array")            //查詢列是數組
        {
            for($i=0;$i<count($A_define);$i++)
            {
                $A_define_up[$i] = trim(strtoupper($A_define[$i]));    //大寫並去除空格
            }
            for($i=0;$i<count($A_define_up);$i++)
            {
                OCIDefineByName($this -> I_stmtID,"$A_define_up[$i]",&$$A_define[$i]);    //綁定
            }
        }
        elseif(trim($A_define) <> "")                //查詢列只有一個
        {
            $A_define_up = trim(strtoupper($A_define));
            OCIDefineByName($this -> I_stmtID,"$A_define_up",&$$A_define);
        }

        //執行綁定好的SQL語句
        if(!OCIExecute($this -> I_stmtID))
        {
            echo "<font color=red><b>執行出錯:</b></font>SQL Error:<font color=red>$C_sql</font><br>";
            return false;
        }

        $lower = 0;                    //返回二維數組的第一維下標控制變量
        $cnt = 0;                    //開始取數標識

        //取記錄
        while (OCIFetchInto($this -> I_stmtID,&$cur,OCI_ASSOC))
        {
            //取查詢出來的所有記錄
            if ($I_start == -1)
            {
                if (gettype($A_define) == "array")        //查詢列是數組
                {
                    for ($i=0;$i<count($A_define);$i++)
                    {
                        if ($cur[$A_define_up[$i]] <> $$A_define[$i])
                        {
                            $$A_define[$i] = $cur[$A_define_up[$i]];     
                        }
                        $A_rs[$lower][$i] = $$A_define[$i];                    //用數字訪問
                        $A_rs[$lower][$A_define[$i]] = $$A_define[$i];        //用小些訪問
                        $A_rs[$lower][$A_define_up[$i]] = $$A_define[$i];    //用大寫訪問
                    }         
                }
                elseif (trim($A_define) <> "")            //查詢列只有一個
                {

                    if ($cur[$A_define_up] <> $$A_define)
                    {
                        $$A_define = $cur[$A_define_up];     
                    }
                    $A_rs[$lower][0] = $$A_define;                    //用數字訪問
                    $A_rs[$lower][$A_define] = $$A_define;        //用小寫訪問
                    $A_rs[$lower][$A_define_up] = $$A_define;    //用大些訪問
                }
                $lower++;            //下標加一
            }

            //取出指定記錄(配合分頁使用)
            if ($I_start <> -1)
            {
                if ($cnt >= $I_start)
                {
                    $cnt++;
                    if ($I_end - $I_start <> 0)
                    {
                        $I_end--;
                            if (gettype($A_define) == "array")
                            {
                                for($i=0;$i<count($A_define_up);$i++)
                                {
                                    if ($cur[$A_define_up[$i]] <> $$A_define[$i])
                                    {
                                        $$A_define[$i] = $cur[$A_define_up[$i]];     
                                    }
                                    $A_rs[$lower][$i] = $$A_define[$i];                    //用數字訪問
                                    $A_rs[$lower][$A_define[$i]] = $$A_define[$i];        //用小些訪問
                                    $A_rs[$lower][$A_define_up[$i]] = $$A_define[$i];    //用大寫訪問
                                }
                            }elseif(trim($A_define) <> "")
                            {
                                if ($cur[$A_define_up] <> $$A_define)
                                {
                                    $$A_define = $cur[$A_define_up];     
                                }
                                $A_rs[$lower][0] = $$A_define;                    //用數字訪問
                                $A_rs[$lower][$A_define] = $$A_define;        //用小些訪問
                                $A_rs[$lower][$A_define_up] = $$A_define;    //用大寫訪問                     
                            }
                        $lower++;
                    }else
                    {
                        break;        //如果$I_end-$I_start=0  表示取完記錄並跳出while循環
                    }     
                }else
                {
                    $cnt++;        //如果$cnt<$I_start,$cnt++
                }                 
            }

        }     //while的結束

        //釋放句柄並返回查詢數據(一個二維數組)
        OCIFreestatement($this -> I_stmtID);
        return $A_rs;      

    } //function的結束
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函數名:db_change($C_sql,$A_bind)
//        作  用:db change
//        參  數:$C_sql                        sql語句
//                $A_bind                        需綁定的字段。數組型         
//        返回值:布爾值
//        備  注:insert,delete,update通用
//------------------------------------------------------------------------------------------
    function db_change($C_sql,$A_bind="")
    {
        if (!$C_sql){AlertExit("參數不全!");}//檢查參數

        //連接檢測
        if($this -> I_linkID==""){    AlertExit("我們的數據庫正忙,請稍後再連接!");}     

        //格式檢測
        $this -> I_stmtID = OCIParse($this -> I_linkID,$C_sql);     
        if (!$this -> I_stmtID){AlertExit(' sql格式出錯!請與程序員聯系');}

        //綁定
        if(gettype($A_bind) == "array")
        {
            for($i=0;$i<count($A_bind);$i++)
            {
                global $$A_bind[$i];
                $$A_bind[$i] = StripSlashes($$A_bind[$i]);            //去掉反斜線字元
                $$A_bind[$i] = str_replace("<?","< ?",$$A_bind[$i]);    //過濾掉PHP標示
            }
            for($i=0;$i<count($A_bind);$i++){
                OCIBindByName($this -> I_stmtID, ":$A_bind[$i]", &$$A_bind[$i], -1);  //綁定
            }
        }
        elseif(trim($A_bind) <> "")                                //不是數組,是字符
        {
            global $$A_bind;
            $$A_bind = StripSlashes($$A_bind);
            $$A_bind = str_replace("<?","< ?",$$A_bind);                //過濾掉PHP標示
            OCIBindByName($this -> I_stmtID, ":$arrBind", &$$A_bind, -1);                 
        }

        //執行並檢測是否成功
        if(!OCIExecute($this -> I_stmtID,OCI_DEFAULT))
        {
            echo "<font color=red><b>執行出錯:</b></font>SQL Error:<font color=red>$C_sql</font><br>";
            return false;
        }

        /*//傳回受影響的行數
        global $I_changenum;
        $I_changenum = OCINumrows($this -> I_stmtID);*/

        //釋放句柄,傳回值
        OCIFreeStatement($this -> I_stmtID);
        return true;
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函數名:db_delete($C_sql)
//        作  用:delete
//        參  數:C_sql                    sql語句
//        返回值:布爾值
//        備  注:該函數只是為了使用直觀,本質調用db_change()
//------------------------------------------------------------------------------------------
    function db_delete($C_sql)
    {
        return $this -> db_change($C_sql);
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函數名:db_insert($C_sql,A_bind)
//        作  用:insert
//        參  數:C_sql                    sql語句
//                A_bind                    綁定
//        返回值:布爾值
//        備  注:該函數只是為了使用直觀,本質調用db_change()
//------------------------------------------------------------------------------------------
    function db_insert($C_sql,$A_bind="")
    {
        return $this -> db_change($C_sql,$A_bind);
    }
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函數名:db_update($C_sql,A_bind)
//        作  用:update
//        參  數:C_sql                    sql語句
//                A_bind                    綁定
//        返回值:布爾值
//        備  注:該函數只是為了使用直觀,本質調用db_change()
//------------------------------------------------------------------------------------------
    function db_update($C_sql,$A_bind="")
    {
        return $this -> db_change($C_sql,$A_bind);
    }
//------------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------------
//        函數名:db_commit()
//        作  用:事務遞交
//        參  數:無
//        返回值:布爾值
//        備  注:無
//------------------------------------------------------------------------------------------     
    function db_commit()
    {
        return    (OCICommit($this->I_linkID));
    }     
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//        函數名:db_rollback()
//        作  用:事務回退
//        參  數:無
//        返回值:布爾值
//        備  注:無
//------------------------------------------------------------------------------------------     
    function db_rollback()
    {
        return  (OCIRollback($this->I_linkID));
    }     
//------------------------------------------------------------------------------------------

     
//------------------------------------------------------------------------------------------
//        函數名:db_logoff()
//        作  用:斷開數據庫連接
//        參  數:無
//        返回值:布爾值
//        備  注:無
//------------------------------------------------------------------------------------------     
    function db_logoff()
    {
        return (OCILogoff($this->I_linkID));
    }
//------------------------------------------------------------------------------------------

     
//------------------------------------------------------------------------------------------
}
?> 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved