Introducing PDO - Another Database Abstraction Layer?
(Page 2 of 2 )
As PDO came to fruition, it was met with no shortage of rumblings from developers either involved in the development of alternative database abstraction layers, or perhaps too focused on PDO’s database abstraction features rather than the entire array of capabilities it offers. Indeed, PDO serves as an ideal replacement for the DB package and similar solutions. However, PDO is actually much more than just a database abstraction layer offering the following:
Coding consistency: Because PHP’s various database extensions are written by a host of different contributors, there is no coding consistency despite the fact that all of these extensions offer basically the same features. PDO removes this inconsistency by offering a single unified interface that is used no matter the database. Furthermore, the extension is broken into two distinct components: the PDO core contains most of the PHP-specific code, leaving the various drivers to focus solely on the data. Also, the PDO developers took advantage of considerable knowledge and experience while building the database extensions over the past few years, capitalizing upon what was successful and being careful to omit what was not.
Flexibility: Because PDO loads the necessary database driver at run time, there’s no need to reconfigure and recompile PHP every time a different database is used. For instance, if your database needs suddenly switch from Oracle to PostgreSQL, just load thePDO_PGSQLdriver and go (more about how to do this later).
Object-oriented features: PDO takes advantage of PHP 5’s object-oriented features, resulting in more powerful and efficient database communication.
Performance: PDO is written in C and compiled into PHP, which, all other things being equal, provides a considerable performance increase over solutions written in PHP.
Given such advantages, what’s not to like? This chapter serves to fully acquaint you with PDO and the myriad of features it has to offer.
Using PDO
PDO bears a striking resemblance to all of the database extensions long supported by PHP; therefore, if you have used PHP in conjunction with a database, the material presented in this section should be quite familiar. As mentioned, PDO was built with the best features of the preceding database extensions in mind, so it makes sense that you’ll see a marked similarity in its methods.
This section commences with a quick overview of the PDO installation process and follows with a summary of its presently supported database servers. For the purposes of the examples found throughout the remainder of this chapter, I’ll use the following table:
CREATE SEQUENCE product_seq start with 1 increment by 1 nomaxvalue;
CREATE TABLE products ( product_id NUMBER PRIMARY KEY, sku CHAR(8) NOT NULL, title VARCHAR2(35) NOT NULL );
The table has been populated with the products shown in Table 23-1.
Table 23-1. Sample Table Data
Product ID
SKU
Title
1
ZP457321
Painless Aftershave
2
TY232278
AquaSmooth Toothpaste
3
PO988932
HeadsFree Shampoo
4
KL334899
WhiskerWrecker Razors
Installing PDO
As mentioned, PDO comes packaged with PHP 5.1 and newer by default, so if you’re running this version, you do not need to take any additional steps. However, because PDO remains under active development, you may want to instead configure it as a shared module. Consult the PHP documentation for more information about this matter.
If you’re using a version older than 5.1, you can still use PDO by installing PECL. However, because PDO takes full advantage of PHP 5’s new object-oriented features, it’s not possible to use it in conjunction with any pre-5.0 release. Therefore, to install PDO using PHP 5 or greater, execute the following:
%>pecl install pdo
Next, enable PDO by adding the following line to yourphp.inifile:
extension=pdo.so
Finally, restart Apache for thephp.inichanges to take effect.
If you’re using PHP 5.1 or newer on the Windows platform, all you need to do is add references to the PDO and driver extensions within thephp.inifile. For example, to enable support for Oracle, add the following lines to the Windows Extensions section:
extension=php_pdo.dll extension=php_pdo_oci.dll
Please check next week for the continuation of this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.