A MySQL Abstraction Class For PHP
Tags: database abstraction,
mysql,
pear,
php
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:
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.
View some statistics:
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.
Subscribe to this Feed
Social bookmark