首頁 > 後端開發 > php教程 > 操作Oracle的php類別實例

操作Oracle的php類別實例

WBOY
發布: 2016-08-08 09:33:43
原創
1015 人瀏覽過

//【警告】:未經許可請勿隨便修改
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
//   
// 【檔案名稱】:                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                //                $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             {
                $A_define_up[$i] = trim(strtoupper($A_define[$i]));    //大寫移除空格
            }
            for($i=0;$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 "執行錯誤:SQL Error:$C_sql
";
            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                     {
                        if ($cur[$A_define_up[$i]] $$A_define[$i])
                        {
                            $$A_define[$i] = $cur[$A_define_up[$i] $$A_define[$i] = $cur[$A_define_up[$i]);                          }
                        $A_rs[$lower][$i] =
                        $A_rs[$lower][$A_define[$i]] =                         $A_rs[$lower][$A_define_up[$i]] = $$A_define[$i];)[$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;    //使用大量存取
] = $$A_define;    //使用大量存取
                }
                $lower++;            //上標加一
            }
             
            //取出指定記錄(配合分頁使用)
            if ($I_start -1)
            {
                if ($cnt >= $I_start)
                {
                    $cnt++;
                    if ($I_end - $I_start 0)
                    {
                        $I_end--;
                            if (gettype($A_define) == "備份")
                            {
                                for($i=0;$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[$5             
                            }
                        $lower++;
                    }else
                    {
                     會                     }     
                }else
                {
                    $cnt++;        //若$cnt                 }                              }
             
        }     //while的結束
         
        //釋放句柄並回傳查詢資料(一個二維數組)
        OCIFreestatement($this -> I_stmtID);
        return $A_rs;      
         
    } //function的結束
//------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
//        函數名稱:db_change($C_sql,$A_bind)
//        作  用:db change
//        蔘  數:$C_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             {
                global $$A_bind[$i];
                $$A_bind[$i] = StripSlashes($$A_bind[$i]);                         $$A_bind[$i] = str_replace("","             for($i=0;$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(""," I_stmtID, ":$arrBind", &$$A_bind, -1);                 }
         
        //執行並偵測是否成功
        if(!OCIExecute($this -> I_stmtID,OCI_DEFAULT))
        {
            echo "執行錯誤:SQL Error:$C_sql
";
            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));
    }
//------------------------------------------------ ------------------------------------------

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

以上就介紹了操作Oracle的php類別實例,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板