Database Abstraction with PDO 

author-image
CIOL Bureau
Updated On
New Update

In PHP, if you want to connect to a MySQL database server, you use the function mysql_connect( ). If you want to connect to an MS SQL Server, you use mssql_connect( ). To issue queries against the databases you use the functions mysql_query( ) and mssql_query( ), respectively. What if you want to make your PHP application portable across databases? A haphazard solution would be a 'find and replace' or else you wrap the database-specific functions within your own generic functions such as connect( ) and query( ). Besides these, several open-source solutions exist to achieve such database abstraction, ADODB
(http://adodb.sourceforge.net) being one of the popular choices.

Advertisment
Direct Hit!
Applies to: PHP developers
USP:
Preview of the upcoming database abstraction library with PHP 5.1 called PHP Data Object or PDO
Primary Link:
www.php.net
Google keywords:
php 5.1 data library

PHP always lacked an out-of-box library for database abstraction, but only until the advent of PDO (PHP Data Objects). PDO earlier existed as a PEAR package (plugin libraries for PHP). But with PHP 5.1, it is a part and parcel of the download bundle. In this article, we take you through installing PHP 5.1 on PCQLinux 2005. Subsequently we see a how to use PDO for database abstraction and look at a couple of its features which makes querying any database more structured and easy for a developer. 

Installation

As an example, we will see how to connect to MS SQL Server running on a Windows machine and MySQL server running on PCQLinux 2005 using PDO. To compile MS SQL support in PHP, you will need to download and install the stable release of FreeTDS drivers available from www.freetds.org. Extract the archive, change to the resultant directory and do a 'make' and then 'make install'.

Advertisment

This will install the FreeTDS drivers in /usr/local. Next, download PHP 5.1 package from www.php.net. Note that PHP 5.1 was in the RC1 (release candidate 1) stage as of this writing. Login as root and extract the archive. This will produce a directory named php-5.1.0RC1. Change to this directory and issue the following.

./configure —prefix=/usr --with-apxs2 --with-pdo-mysql --with-pdo-dblib=/usr/local --with-xmlrpc

make

make install

Note that we have only compiled in PDO support (drivers) for MySQL (using the —with-pdo-mysql option) and for MS SQL Server (using the —with-pdo-dblib) option. Depending on your requirement, you may like to compile in support for other databases. Issue './configure --help' which will list down the options to compile PDO drivers for other databases. Next, fire up a text editor and type in the following.

Advertisment

AddType application/x-httpd-php .php

DirectoryIndex index.php

Save the file as php.conf in/etc/httpd/conf.d. In case you have installed PHP that comes with PCQLinux 2005, you will already have a file named php.conf in this directory. In that case, open the file and comment the following line by prefixing a #.

LoadModule php4_module modules/libphp4.so

Advertisment

Restart Apache Web server.

Read more at PCQuest.

tech-news