Database Code

  Home arrow Database Code arrow MySQL/MSSQL abstraction Layer
DATABASE CODE

MySQL/MSSQL abstraction Layer
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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

    By : jcostom

    <?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);
    }
    }

    ?>
    /* example usage
    <?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";
    }
    ?>
    */
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap