Zend - The PHP Company




Abstraction Layers

Add Code


MySQL/MSSQL abstraction Layer  

Type: class
Added by: jcostom
Entered: 02/03/2000
Last modified: 08/12/1999
Rating: **** (5 votes)
Views: 11120
I needed a quick way to switch quickly between MySQL and MS SQL Server for applications, so I wrote two classes, that provide a modular, consistent API that can be used for either database. Changing out your DB is as easy as dropping in the proper DB_whatever.php file into the dir that DB.php lives in, then changing the $dbtype var before you require() DB.php


<?php

/*

Copyright (c) 2000, Jason Costomiris
All rights reserved.

Don't be scared, it's just a BSD-ish license.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by Jason Costomiris.
4. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/



/* This section should be in a file named DB.php */

class DB_base {
    
// get server/instance
    
function setinst($instance){
        
$this->instance $instance;
    }

    
// get username
    
function setuser($user){
        
$this->user $user;
    }

    
// get password
    
function setpass($pass){
        
$this->pass $pass;
    }

    
// get database name
    
function dbname($dbname){
        
$this->dbname $dbname;
    }

    
// Use persistent connections?
    // Only call this if you want persistent connections
    
function persist(){
        
$this->persist 1;
    }

}

$file join("", array("DB_"$dbtype".php"));
require(
$file);

?>


<?php

/* This section should be in a file named DB_mysql.php */

class DB extends DB_base {
                
    
// Connect or pconnect.
    
function connect(){
        if(
$this->persist){
            
$this->conn =
            
mysql_pconnect($this->instance$this->user$this->pass);
        } else {
            
$this->conn 
            
mysql_connect($this->instance$this->user$this->pass);
        }
        
mysql_select_db($this->dbname$this->conn);
        return(
$this->conn);
    }

    
// close
    
function close(){
        if(
$this->persist) {
            
$ret 1;
        } else {
            
$ret mysql_close($this->conn);
        }
        return(
$ret);
    }

    
// query
    
function query($query){
        
$this->result mysql_query($query);
        return(
$this->result);
    }
                
    
// numrows
    
function numrows(){
        
$this->numrows mysql_num_rows($this->result);
        return(
$this->numrows);
    }

    
// affected rows
    
function affrows(){
        
$this->affrows mysql_affected_rows($this->result);
        return(
$this->affrows);
    }

    
// seek
    
function seek($row){
        
$seek mysql_data_seek($this->result$row);
        return(
$seek);
    }

    
// fetch object
    
function fobject(){
        
$object mysql_fetch_object($this->result);
        return(
$object);
    }

    
// fetch array
    
function farray(){
        
$array mysql_fetch_array($this->result);
        return(
$array);
    }

    
// free
    
function free(){
        
$free mysql_free_result($this->result);
        return(
$free);
    }
}

?>


Usage Example


<?php
    $dbtype 
"mysql";
    require(
"DB.php");
    
$db = new DB;
    
$db->setinst("dbserver");
    
$db->setuser("dbusername");
    
$db->setpass("dbpassword");
    
$db->dbname("dbname");
    
$db->persist();
    
$conn $db->connect();
    
$db->query("SELECT id,narf FROM foo WHERE id > 4");
    while(
$r $db->fobject()){
        echo 
"$r->id$r->narf<br>n";
    }
?>


Rate This Script





Search



This Category All Categories