[BACK]
PHP Database Classes For Oracle and Mysql
Summary This module provides customized interfaces for php code to perform all database operations. Two classes are defined in this module to provide connection to Oracle database and Mysql database. This module hides all database operation detail from programmer. The following steps are true for all database queries using this module.
  • Create an instance of the class.
  • Call DBParse() method.
  • Call DBExecute() method.
  • Call DBFetchall() method.
  • Release the connection.

Usage Examples Query all employees' name whose age are older than 45.
 
Oracle Connection Module
<?php
    $username="acme";
    $password="acme";
    $oracle_db_name="oracle.bogusdomain.com";
    $dbobj = new DB_Handler($username,$password,$oracle_db_name);
    $query = "Select name From Employees where age>:1";
    $dbobj->DBParse($query);
    undef($bindVar);
    $bindVar[1] = 45;
    $dbobj->DBExecute($bindVar);
    $result=$dbobj->DBFetchall();
    foreach($result as $x){
         print "$x[0]";
    }
    $dbobj->DBFree();

    /*Note: Always provide a bind variable array to DBExecute(). 
    If no bind variable is needed, pass an empty array.*/
?>

MySQL Connection Module
<?php
	$username="acme";
	$password="acme";
	$hostname="mysql.bogusdomain.com";
	$mysql_db_name="company";
        $dbobj = new MySql_Handler($username,$password,$hostname,$mysql_db_name);
	$query = "Select name From Employees where age>45";
        $dbobj->DBParse($query);
        $dbobj->DBExecute();  //No bind variable array is needed.
        $result=$dbobj->DBFetchall();
        foreach($result as $x){
              print "$x[0]";
        }
        
       
        /*---------Alternate approach------------
        for($i=0;$i<$dbobj->DBRowCount();$i++){
              print $result[$i][0];
        }
	-----------------------------------------*/
        $dbobj->DBFree();
?> 
PS: You can use DBFetchone() method. However, signature for Oracle and MySQL is different.


Codes
Oracle and MySQL database Connection Class Module
<?php
/*-------------------------------------------------
Database Module for Oracle.
----------------------------------------------------*/
class DB_Handler {
    var $result;
    var $DBConn;
    var $stmt;
    var $empty_res;

    //Constructor
    function DB_Handler($username,$password,$dbname){
	$this->DBConn = @OCILogon($username,$password,$dbname);
    }

    /*Check if connection is valid. 
    Useful for handling database error.*/
    function DBCheckConn(){
        $flag = false;
        if($this->DBConn == null){
            $flag = false;
        }else{
            $flag=true;
        }
        return $flag;
    }

    //Create statement's query
    function DBParse($query){
        $this->stmt = OCIParse($this->DBConn, $query);
        $errorArr = OCIError($this->stmt);
        if ($errorArr['code']) {
             print $errorArr['message'];
             exit;
        }
    }
        
    //Execute query.
    function DBExecute($var_arr){
        $size = count($var_arr);
        $f = "";
        if ($size>0){
             for ($i=1;$i<=$size; $i++){
                 $f = ":$i"; 
                 if(!is_numeric($var_arr[$i])){
                     $var_arr[$i] = 
                        preg_replace("/\\\+/","",$var_arr[$i]);                        
                 }
                 OCIBindByName($this->stmt, $f,$var_arr[$i],-1); 
             }
        }
        OCIExecute($this->stmt,OCI_DEFAULT);
        $errorArr = OCIError($this->stmt);
        if ($errorArr['code']) {
              print $errorArr['message'];
              exit;
        }
    }
    
    //Return query results in 2D array.
    function DBFetchall(){
        $output=array(); 
        $rows =OCIFetchStatement($this->stmt, $resultArr);
        $cols = 0;
        while (each($resultArr)){
            $cols = $cols +1;
        }
        for ($i=0;$i<$rows;$i++){
            reset($resultArr);
            unset($one_row);
            while ($column = each($resultArr)){
                $data = $column['value'];
                $one_row[] = $data[$i];
            }
            $output[$i] = $one_row;
        }
        return $output;
    }

    //Get the first row from this result set.
    function DBFetchone($two_dim_rs){
        $result = "";
        if($two_dim_rs){
           foreach($two_dim_rs as $x){
               if($x){
                  $result = $x;
                  break;
               }
           } 
        }
        return $result;
    }

    /*Commit database changes after 
    insert or update.*/
    function DBCommit(){
        if(OCICommit($this->DBConn)){
              $success = true;
        }else{
              $success = false;
              OCIRollBack($this->DBConn);
        }
        return $success;
    }
     
    //Release database connection.
    function DBFree(){
        OCILogOff($this->DBConn);
    } 
}

/*-------------------------------------------------
Database Module for MySQL.
----------------------------------------------------*/
class MySql_Handler {
    var $result;
    var $DBConn;
    var $stmt;
    var $empty_res;
    var $ret_result;
    var $row_count;
    var $sel_query;

    //Constructor
    function MySql_Handler($username,$password,$host, $dbname){
           $this->DBConn = mysql_connect($host, $username,$password);
             mysql_select_db($dbname, $this->DBConn);
    }

    //Release database connection.
    function DBFree(){
        mysql_close($this->DBConn);
    }

   //Validate database connection.
   function DBCheckConn(){
       $flag = false;
       if($this->DBConn == null){
           $flag = false;
       }else{
           $flag=true;
       }
       return $flag;
    }

    //Create statement's query
    function DBParse($query){
       $this->stmt = trim($query);
       if(!strcmp("select",strtolower(substr($this->stmt,0,6))) ){
            $this->sel_query = TRUE; 
       }else{
            $this->sel_query = FALSE; 
       }
    }
        
    /*Execute query. There is not binding 
    variable array in method signature.*/
    function DBExecute(){
        $this->ret_result=null;
        $this->result = mysql_query($this->stmt) or 
                     die("Invalid query: " . mysql_error()) ; 
        if($this->sel_query){
              $this->row_count = mysql_num_rows($this->result);
        }
    }

    //Return query results in 2D array.
    function DBFetchall(){
        if(($this->ret_result==null) && ($this->row_count!=0)){
              $this->ret_result=array(); 
              $i = 0;
              while ($line = mysql_fetch_array($this->result, MYSQL_BOTH)) {
                    $this->ret_result [$i] = $line;
                    $i++;       
              }
              return $this->ret_result;
        }else{
              return -1;
        }
    }

    /*Return first row in the result set. 
    Note, no argument is needed in method signature.*/
    function DBFetchone(){
        if(($this->ret_result==null) && ($this->row_count!=0)){
             $this->ret_result=array(); 
             $i = 0;
             while ($line = mysql_fetch_array($this->result, MYSQL_BOTH)) {
                  $this->ret_result [$i] = $line;
                  $i++;        
             }
             return $this->ret_result[0];
        } else{
             return -1;
        }
    }

    //Return number of rows in the result set.
    function DBRowCount(){
         return $this->row_count;
    }

    //Start transactions.
    function DBBegin(){
       mysql_query("BEGIN",$this->DBConn);
    }

    //Commit changes for insert or update.
    function DBCommit(){
        mysql_query("COMMIT",$this->DBConn);
        $success =1;
        return $success;
    } 
 
    //Revert the changes.
    function DBRollBack(){
        mysql_query("ROLLBACK",$this->DBConn);
    }      
}
?>
                          
Hosted by www.Geocities.ws

1