A Database-Independent API for PHP
By Sterling Hughes
PHP is a valuable item in any Web developer's toolbox. The server-side technology allows for rapid development of dynamic, database-driven applications. One of PHP's major strengths is its ability to connect with many different databases, including Sybase, PostgreSQL, Oracle, or MySQL.
Database connectivity, however, is also one of PHP's major weaknesses. Specifically, PHP's lack of a standard database API makes switching databases difficult and costly once an application has been written. For example, if you write a program that uses a MySQL database on the back end, and you want to modify that program so that it uses a Sybase database, you'll most likely spend hours modifying the code.
Highest Form of Flattery
Fortunately, there are several workarounds to this problem, including a couple that rely on new features in PHP4. A lot can be learned from examining (and borrowing) a solution from Perl. For the particular solution presented here, I've used Perl's DBI as a model. Before Perl 5, many Perl programmers faced the same problem that PHP programmers face today. They had to use different modules and subroutines to access different databases. For example, the sybperl module worked with Sybase, while oraperl interfaced with Oracle. Switching databases meant going through the code and changing all references from sybperl to oraperl. Perl programmers got frustrated and began looking for a better solution. The result was the DBI module.
DBI lets you access many different databases with the same set of methods.