Tony Landis
Personal website of Tony Landis

30 May 08 Convert from ADOdb Library to PDO

In the past I used the ADOdb Database Abstraction Library for PHP for a lot of my PHP projects. However, PDO (PHP Data Objects) is now a better fit as it performs better for me and doesn’t require the inclusion of any libraries.

I wanted to make the switch not only for future projects, but also to go back into some of my existing projects that I maintain and covert from ADOdb to PDO. However, I didn’t want to alter thousands of calls to ADOdb methods.

The solution I came up with was to do some more work on my PDO Singleton Class. By adding in the ADOdb methods Execute(), qstr(), GetOne(), and an iterator for the adodb recordsets returned by Execute(), I was able to easily transition to PDO in projects with thousands of lines of code by including this class and creating a new instance of sdb() instead of NewADOConnection() and Connect().

$DB =& new sdb(”mysql:host=localhost;dbname=testdb”, ‘username’, ‘password’);

This is the equivalent of the following ADOdb code: (except it is a singleton)

$DB = NewADOConnection(’mysql’);
$DB->Connect(”localhost”, “username”, “password”, “testdb”);

You can now treat that $DB object just as you would with ADOdb to create a connection to the mysql database ‘testdb’. Note: I have not refactored the error or transaction functions as they were not used in the specific projects I needed this conversion in… but they would be very simple to add.


Tags: , , ,

23 May 07 PHP5 PDO Singleton Class

I use Zend Studio for Eclipse as my PHP IDE. The framework I am developing uses PHP PDO for the database abstraction, and rather than passing around a reference to my PDO instance I wanted to create a singleton.

At the same time, I wanted to take advantage of the helpful code completion and suggestion features of the Zend IDE. So I created a class that creates a PDO singleton object in a way that Zend Studio for Eclipse can still do code assistance.

PHP5 PDO Singleton Usage Example

$dbObj =& new sdb("mysql:host=localhost;dbname=testdb", 'username', 'password');

This create a connection to the mysql database ‘testdb’ and a new PDO instance. $dbOjb can be passed around by reference but using the sdb:: singleton is recommended as Zend will do code assist on this.

Tags: , , ,