In this first article of a five-part series, you'll learn about database abstraction layers: why they exist and what they're used for. You'll also be introduced to the PHP Data Objects (PDO) abstraction layer. This article is excerpted from chapter 23 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
While all mainstream databases adhere to the SQL standard, albeit to varying degrees, the interfaces that programmers depend upon to interact with the database can vary greatly (even if the queries are largely the same). Therefore, applications are almost invariably bound to a particular database, forcing users to also install and maintain the required database if they don’t already own it or, alternatively, to choose another possibly less capable solution that is compatible with their present environment. For instance, suppose your organization requires an application that runs exclusively on PostgreSQL, but your organization is standardized on Oracle. Are you prepared to invest the considerable resources required to obtain the necessary level of PostgreSQL knowledge required to run in a mission-critical environment and then deploy and maintain that database throughout the application’s lifetime?
To resolve such dilemmas, enterprising programmers began developing database abstraction layers, which serve to decouple the application logic from that used to communicate with the database. By passing all database-related commands through this generalized interface, it became possible for an application to use one of several database solutions, provided the database supported the features required by the application and the abstraction layer offered a driver compatible with that database. A graphical depiction of this process is found in Figure 23-1.
Figure 23-1.Using a database abstraction layer to decouple the application and data layers
It’s likely you’ve heard of some of the more widespread implementations, a few of which are listed here:
MDB2: MDB2 is a database abstraction layer written in PHP and available as a PEAR package. (See Chapter 11 for more information about PEAR.) It presently supports FrontBase, InterBase/Firebird, MySQL, Oracle, PostgreSQL, and SQLite. If you require support for Informix, Mini SQL, ODBC, or Sybase, see the PEAR DB package, which was MDB2’s precursor.
JDBC: As its name implies, the Java Database Connectivity (JDBC) standard allows Java programs to interact with any database for which a JDBC driver is available. Drivers are available for MSSQL, MySQL, Oracle, and PostgreSQL, among others.
ODBC: The Open Database Connectivity (ODBC) interface is one of the most widespread abstraction implementations in use today, supported by a wide range of applications and languages, PHP included. ODBC drivers are offered by all mainstream databases, including those referenced in the previous JDBC introduction.
Perl DBI: The Perl Database Interface (DBI) module is Perl’s standardized means for communicating with a database and was the inspiration behind PHP’s DB package.
As you can see, PHP offers DB and supports ODBC; therefore, it seems that your database abstraction needs are resolved when developing PHP-driven applications, right? While these (and many other) solutions are readily available, an even better solution has been in development for some time and has been officially released with PHP 5.1. This solution is known as the PHP Data Objects (PDO) abstraction layer.