Home >Backend Development >PHP Tutorial >Operating Oracle's php class instance

Operating Oracle's php class instance

WBOY
WBOYOriginal
2016-08-08 09:33:431021browse

//[Warning]: Please do not modify without permission
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
//
// [File name]:                                                                                                                                                                                                                                                                                             // [Function]: Oracle public function class
//
// [Last modification date]: 2001/05/11[cxx]
// [Variable definition rules]: ‘C_’ = character type, ‘I_’ = integer type, ‘N_’ = numeric type, ‘L_’ = Boolean type, ‘A_’ = array type
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
// ※db_logon() Open database connection
// ※db_query()                                                                                                                                       // ※ DB_CHANGE () Database changes universal functions (Insert, Delete, Update)
// ※db_insert () // ※db_delete()                                                                                                              to delete, directly call db_change()
// ※ db_update () update, directly call db_change ()
// ※db_commit()                                                                                                                                       // ※db_rollback() ※Transaction rollback
// ※db_logoff() Disconnect the database connection
//------------------------------------------------ ------------------------------------------


Class c_ora_db
{

​​​
//------------------------------------------------ ------------------------------------------
// Variable definition
//------------------------------------------------ ------------------------------------------
                                                                                                                                                                                      = ""                                                                                                                         = ""                                                                                                                                                                                                                      = "" var $I_linkID = 0;                                                                                                               var $I_stmtID = 0; var $I_stmtID                                                                                                                                                            //------------------------------------------------ ------------------------------------------
​​​


//------------------------------------------------ ------------------------------------------
// Function name: db_logon()
// Function: Open database connection
// Parameters: None
// Return value: connection handle (integer)
//                   Note: None
//------------------------------------------------ ------------------------------------------
Function db_logon()
                                                                $this->I_linkID = @OCILogon($this->C_user,$this->C_passwd,$this->C_db);
If ($this->I_linkID == 0){AlertExit('Database link failed, please contact the DBA!');}
Return $ this-& gt; i_linkid;
}
//------------------------------------------------ ------------------------------------------
​​​​
​​​​
//------------------------------------------------ ------------------------------------------
// Function name: db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
// Function: select
// Parameters: $C_sql sql statement
// $ a_define to be binded. Array type
// $ i_start Start a record -1 and take out all the records of the query
// $ i_end End Records
//         Return value: two-dimensional array ($A_rs)
//              Note: The value of the corresponding field can be accessed through the numbers 0,1,2....; or the value of the corresponding field can also be accessed by querying the field name
//                                                                                                                                                 being being able to access the NAME field of the first record via $A_rs[0][0] or $A_rs[0]['NAME'] or $A_rs[0]['name']
//                 $I_start, $I_end are parameters used with paging.
//------------------------------------------------ ------------------------------------------
Function db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
                                                       If (!$C_sql){AlertExit("Incomplete parameters!");}//Check parameters
​​​​
​​​​//Connection detection
If ($this->I_linkID == 0){AlertExit('Database link failed, please contact the DBA!');}
​​​​
​​​​//Format detection
         $this -> I_stmtID = OCiparse($this -> I_linkID,$C_sql);                                                     If (!$this -> I_stmtID){AlertExit('SQL format error! Please contact the programmer');}
​​​​
                //If the bound field is not specified, it will be fetched from the SQL statement
         if($A_define=="")
                                                                          $A_Cur = explode("select",$C_sql);
               $A_Cur = explode("from",$A_Cur[1]);
               $A_define = explode(",",$A_Cur[0]);
         }
​​​​
//Bind database table fields
If(gettype($A_define) == "array") //The query column is an array
                                                            for($i=0;$i                                                                                  $A_define_up[$i] = trim(strtoupper($A_define[$i]));                   }
for($i=0;$i                                                                 OcidefineByname ($ this -& gt; i_stmtid, "$ a_define_up [$ i]", & $ $ a_define [$ i]); // Bind

                  }
           }
          elseif(trim($A_define) <> "")                                                                                                                                                                                                                                                                                                                                                      $A_define_up = trim(strtoupper($A_define));
                OCIDefineByName($this -> I_stmtID,"$A_define_up",&$$A_define);
           }

​​​​//Execute the bound SQL statement
If(!OCIExecute($this -> I_stmtID))
                                                                                                                                                                   echo "Execution error:SQL Error:$C_sql
";
                   return false;
           }
​​​​​
                                                                                                                                                                                                            ​​​​​
​​​​//Get records
​​​​​while (OCIFetchInto($this -> I_stmtID,&$cur,OCI_ASSOC))
                                                                                      // Get all the records queried
                 if ($I_start == -1)
                                                                If (gettype($A_define) == "array") //The query column is an array
                                                                   for ($i=0;$i                                                                         If ($cur[$A_define_up[$i]] <> $$A_define[$i])
                                                                                                                                                                                                                                                                                                                                                    $$A_define[$i]                                                                                                       $ A_RS [$ LOWER] [$ i] = $ $ a_define [$ i]; // Use digital access
                         $A_rs[$lower][$A_define[$i]] = $$A_define[$i];                           $A_rs[$lower][$A_define_up[$i]] = $$A_define[$i];                                                                                                                                                                                                    elseif (trim($A_define) <> "")                                                                                                                                                                                         elseif (trim($A_define) <>                                                                                                                          If ($cur[$A_define_up] <> $$A_define)
                                                                                                                                                                                                                                                                                                                                                                                                               $ A_RS [$ LOWER] [0] = $ $ a_define; // Use digital access
                                                                                                                                                                                                                                                                              $A_rs[$lower][$A_define] = $$A_define;                             $A_rs[$lower][$A_define_up] = $$A_define;                                                                                                           $lower++;                                                                                                                                                                                                                                                                   $lower++;                   }
                             
​​​​​​ //Retrieve specified records (used with paging)
                   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                                 {
                                    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;                    //用数字访问
                                                                                                                                                                                                                                                                   through $A_rs[$lower][$A_define] = $$A_define;                                                                                                                                                                                                                    $A_rs[$lower][$A_define_up] = $$A_define;                                                                                               $lower++;
else                                                                                                                                                                                                                                     break;                                                                                  else                                                                                                                                                                                                                                                                                                                     $cnt++;                                                                                                                          }
                             
                                      //End of while
​​​​​
​​​​​//Release the handle and return the query data (a two-dimensional array)
OCIFreestatement($this -> I_stmtID);
          return $A_rs; ​​​​​
} //End of function
//------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
// Function name: db_change($C_sql,$A_bind)
// Function: db change
// Parameters: $C_sql sql statement
// $ a_Bind to bind the field. Array type
//        Return value: Boolean value
//                                                                                                                                                                                                                           NOTE //------------------------------------------------ ------------------------------------------
Function db_change($C_sql,$A_bind="")
{
If (!$C_sql){AlertExit("Incomplete parameters!");}//Check parameters
​​​​​
​​​​//Connection detection
          if($this -> I_linkID==""){       AlertExit("Our database is busy, please try again later!");}                                                                                                                                               ​​​​​
             //Format detection
            $this -> I_stmtID = OCIParse($this -> I_linkID,$C_sql);                                                         If (!$this -> I_stmtID){AlertExit('SQL format error! Please contact the programmer');}
​​​​​
​​​​//Binding
If(gettype($A_bind) == "array")
                                                            for($i=0;$i                                                                                        global $$A_bind[$i];
$o 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); //Binding
                  }
           }
          elseif(trim($A_bind) <> "")                                                                                                                                                                                 global $$A_bind;
                 $$A_bind = StripSlashes($$A_bind);
$ $ A_bind = strplace ("" & lt;? "," & Lt;? ", $ $ A_bind); //               OCIBindByName($this -> I_stmtID, ":$arrBind", &$$A_bind, -1);                                                 }
​​​​​
​​​​//Execute and check whether it is successful
If(!OCIExecute($this -> I_stmtID,OCI_DEFAULT))
                                                                                                                                                                   echo "Execution error:SQL Error:$C_sql
";
                   return false;
           }
​​​​​
​​​​/*//Return the number of affected rows
​​​​​global $I_changenum;
          $I_changenum = OCINumrows($this -> I_stmtID);*/
​​​​​
​​​​//Release the handle and return the value
OCIFreeStatement($this -> I_stmtID);
          return true;
}
//------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
// Function name: db_delete($C_sql)
// Function: delete
// Parameters: C_sql sql statement
//        Return value: Boolean value
//            Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
Function db_delete($C_sql)
{
           return $this -> db_change($C_sql);
}
//------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
// Function name: db_insert($C_sql,A_bind)
// Function: insert
// Parameters: C_sql sql statement
// A_bind Bind
//        Return value: Boolean value
//            Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
Function db_insert($C_sql,$A_bind="")
{
           return $this -> db_change($C_sql,$A_bind);
}
//------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
// Function name: db_update($C_sql,A_bind)
// Function: update
// Parameters: C_sql sql statement
// A_bind Bind
//        Return value: Boolean value
//            Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
Function db_update($C_sql,$A_bind="")
{
           return $this -> db_change($C_sql,$A_bind);
}
//------------------------------------------------ ------------------------------------------



//------------------------------------------------ ------------------------------------------
// Function name: db_commit()
// Function: Transaction submission
// Parameters: None
//        Return value: Boolean value
//                   Note: None
//------------------------------------------------ ---------------------------------------------
Function db_commit()
{
Return (Ocicommit ($ this-& gt; i_linkid));
                                                                 //------------------------------------------------ ------------------------------------------


//------------------------------------------------ ------------------------------------------
// Function name: db_rollback()
// Function: transaction rollback
// Parameters: None
//        Return value: Boolean value
//                   Note: None
//------------------------------------------------ ---------------------------------------------
Function db_rollback()
{
Return (Ocirollback ($ this-& gt; I_linkid));                                                                              //------------------------------------------------ ------------------------------------------

​​​
//------------------------------------------------ ------------------------------------------
// Function name: db_logoff()
// Function: Disconnect the database connection
// Parameters: None
//        Return value: Boolean value
//                  Note: None
//------------------------------------------------ ---------------------------------------------
Function db_logoff()
{
           return (OCILogoff($this->I_linkID));
}
//------------------------------------------------ ------------------------------------------

​​​
//------------------------------------------------ ------------------------------------------
}
?>
The above introduces the PHP class examples for operating Oracle, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn