PHP
  Home arrow PHP arrow Page 2 - Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-01-17


    Table of Contents:
  • Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5
  • Creating the first abstraction layer for MySQL
  • Creating a bridge class with PHP 5
  • Putting the MySQL abstraction layer to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5 - Creating the first abstraction layer for MySQL
    ( Page 2 of 4 )

    As I explained in the introduction, my intention here is to develop a simple abstraction layer by using only a bridge class and a couple of "bridged" objects. The purpose of doing this is basically to have access to the MySQL server by utilizing either the conventional "mysql" extension, or the highly-improved "mysqli" library that comes bundled with PHP 5.

    Having explained how I plan to use the bridge design pattern in this specific case, let me show you the first class involved in this example. It consists of a simple wrapper for MySQL, and logically uses the respective "mysql" extension as well. Here is the signature for this brand new class. Please have a look at it: 

    // define 'MySQL' class class MySQL{ public function __construct($host,$user,$password,$database){ if(!$conId=mysql_connect($host,$user,$password)){ throw new Exception('Error connecting to
    the server'); }         if(!mysql_select_db($database,$conId)){ throw new Exception('Error selecting
    database'); } } // run query public function query($query){ if(!$this->result=mysql_query($query)){ throw new Exception('Error performing query '.$query);         } return new Result($this,$this->result); } } // define 'Result' class class Result { private $mysql; private $result; public function __construct(&$mysql,$result){         $this->mysql=&$mysql;         $this->result=$result; } // fetch row public function fetch_array(){         return mysql_fetch_assoc($this->result); } // count rows public function countRows(){      if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); }         return $rows; } // count affected rows public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected
    rows'); } return $rows; } // get ID form last-inserted row public function getInsertID(){         if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } // seek row public function seekRow($row=0){         if(!is_int($row)||$row<0){ throw new Exception('Invalid result set
    offset');         }         if(!mysql_data_seek($this->result,$row)){ throw new Exception('Error seeking data');         } } }

    As you can see, there's nothing special with reference to the MySQL wrapping classes listed above. Similar to many abstraction classes that you have possibly used in many web applications, these allow you to perform some simple operations, such as connecting to the server, selecting a specific database, running queries, and processing result sets in a few useful ways.

    However, the pair of MySQL wrapping classes that you just saw definitely aren't the main topic of this article. I defined them here only with the purpose of creating another application that lets you access the MySQL server, either with the traditional "mysql" extension, or the improved "mysqli" library.

    In this case, the first requirement has been already satisfied, since the previous classes use the conventional extension. Therefore, it's time to move on and see how to develop a bridge class which will allow us to use MySQL by utilizing the two mentioned libraries.

    Things are getting really exciting now! Therefore, if you want to learn how this bridge class will be created, please keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek