A MySQL Abstraction Class For PHP

Tags: , , ,
Posted 31. December 2006.

Over the time I have been working with PHP, I built a library of useful classes for my projects. I have always wanted to publish this library freely. It will take some time to clean up the code base and comments, but if someone has use for it, then I will be glad to do it.

Here is the first part of this series: A fast and lightweight DB abstraction class with a MySQL driver included. The syntax is one-way compatible to PEAR's DB class, so switching from PEAR::DB to this implementation is easy.

The main reason why I coded this class is performance. PEAR is just way to much overkill if you just need easier handling and abstraction of PHP's MySQL functions. As it is basically just a wrapper class, it's almost as fast as PHP's native MySQL functions. You don't have to do benchmarks to feel the speed improvement over PEAR!

Main features of phplib_DB 1.0:

  • Easy, one-way PEAR compatible syntax. It's like PEAR::DB without all the stuff you are never going to need anyway.
  • Table prefixing. Useful if you're on a shared host with limited databases.
  • Statistics about time and number of queries executed.
  • Object-oriented code: PHP 5 required.

Here's an example of how to use it:

<?php
include("path/to/phplib_DB/DB.php");
$db = DB::connect("mysql://user:password@localhost/database");
$result = $db->query("SELECT name, age FROM pets WHERE id = 1");
list($name, $age) = $result->fetchRow();
?>

Table prefixing works like this: the string '#_' will be replaced with your predefined prefix.

define('DB_table_PREFIX', 'prefix_');
$sql= "SELECT name, age FROM #_pets WHERE id = 1";

View some statistics:

// Print number of queries
echo $db->statQueries();
// Print time consumed for all queries
echo $db->statTime();

Download the source code here:phplib_DB-1.0.tar.gz

Documentation is not yet included but the source explains a lot and most functions have the same names as in PEAR::DB.

Please feel free to ask if you have any questions.

SubscribeSubscribe to this Feed
del.icio.usSocial bookmark

Comment on this article [2]

  1. user said 671 days ago:

    thank you.. this is very helpfull

  2. jooria said 201 days ago:

    you can add it here as a project
    http://www.jooria.com/scripts/PHP-Class-Scripts-129/

Commenting is closed for this article.