Spread your database connections with PHP PDO
Less is more
PHP is one of the most commonly used scripting languages on the web - about 35 per cent of websites use PHP. Databases, meanwhile, are undergoing something of a renaissance thanks to web development.
Often one database is used during the development stage of a web application and another database is used in production - MySQL could be used for the former and Oracle for the latter. Reasons for this vary: it could be the availability of a particular database from the PHP web host our your own personal preference as a programmer.
MySQL, Oracle, IBM's DB2 UDB and Microsoft's SQL Server 2005 may be connected using the PHP extensions. Each of the PHP extensions is database specific, though, and the PHP functions used by the different extensions are quite different. Consider the application that uses MySQL in development and Oracle in production: in this scenario, the PHP application won't run on the Oracle database.
That's where the PHP Data Object extension steps in. PHP PDO provides an interface for connecting to many different databases using the same set of PHP functions. The advantage of the PDO extension is that it provides generic connectivity with RDBMS databases. Without the PHP PDO extension a different set of PHP functions would have to be used for the different databases.
In this article I shall explain the simplest way you can connect to a range of popular databases, tailored to developers, using the PHP PDO extension. I shall use MySQL 5.0 Community Server, IBM DB2 9 Trial Version and SQL Server 2005 Express Edition with Oracle's database 10g Standard Edition as I am Oracle certified associate, although Express Edition may be used just as well.
Before we get started, we need to install the PDO extension. PDO provides a data-access abstraction layer. The PHP 5 distribution is packaged with the PDO extension. Activate the PDO extension by adding the following extension directive to the
php.ini configuration file.
We also need to activate the database-specific PDO drivers in
php.ini. We shall use the PDO extension with the SQL Server 2005, MySQL, Oracle, and IBM's DB2 UDB databases.
extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll extension=php_pdo_oci.dll extension=php_pdo_odbc.dll
The PDO ODBC driver is used to connect with the DB2 UDB database. The
ntwdblib.dll included with PHP 5 does not connect to SQL Server 2005 from a PHP. Obtain a newer
ntwdblib.dll from here and copy
C:/PHP directory. Restart the Apache2 server.
The PDO constructor is used to obtain a connection with a database. The data source name for the different databases-specific drivers is specified as a parameter to the PDO constructor. As you shall notice, only the connection parameters to obtain a connection with the database are different for each database. The procedure to create a table and retrieve data is the same for each of the databases and the same set of functions is used.
MySQL 5.0 Community Server
In a PHP script
createMySQLTable_PDO.php create a connection with the MySQL database using the PDO constructor and
mysql PDO driver. Specify the connection parameters for the MySQL database;
port as 3306 and
test. The connection may be set to be a persistent connection using the
$connection = new PDO('mysql:host=localhost;port=3306;dbname=test', $user, $password, array(PDO::ATTR_PERSISTENT => true));
Next, run SQL statements to create a MySQL database table using the
To create a MySQL table run the PHP script with the URL: http://localhost/createMySQLTable_PDO.php.
To retrieve data from the MySQL database, in a PHP script
retrieveMySQLData_PDO.php, create a connection with the MySQL database using the PDO constructor. Prepare an SQL statement to run an SQL query using the
prepare() function. Parameter markers may be used in a prepared statement. Specify a parameter marker for the CATALOGID column.
$stmt = $connection->prepare("SELECT * from CATALOG WHERE CATALOGID=?");
Set the cursor to be scrollable for the result set returned by the SQL query using the
$stmt ->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL);
Bind a PHP variable for the
catalogId value to the parameter marker using the
$catalogId='catalog1'; $stmt->bindParam(1, $catalogId);
Run the prepared statement using the
Retrieve a row of data from the result set using the
fetch() function. Iterate over the result set and add the data retrieved to the HTML table.
We shall use the
oci PDO driver to connect with the Oracle database. Create a PHP script,
createOracleTable_PDO.php. Create a connection with the Oracle database using the PDO constructor. The only difference from obtaining a connection with MySQL database is that the
oci PDO driver is used instead of the
mysql PDO driver.
$db = "oci:dbname=ORCL;host=localhost;port=1521"; $connection = new PDO($db, $user, $password);
Similar to the MySQL, database run SQL statements to create a database table, add data to the database table and retrieve data from the table. The
query() function may be used to run an SQL query instead of a prepared statement.
IBM DB2 9 Trial Version
We shall use the
odbc driver for PDO to connect with the DB2 database. Create a PHP script
retrieveDB2Table_PDO.php. Create a connection with the DB2 database using the PDO constructor and the ODBC driver.
$connection = new PDO('odbc:SAMPLE', $user, $password);
The procedure to create a database table, and for adding data to and for retrieving data from the table is the same as MySQL and Oracle. The advantage of the PDO class library is that PHP scripts that may be run with any database by just modifying the connection parameters for the database specific PDO driver in the PDO constructor.
SQL Server 2005 Express Edition
We shall use the SQL Server PDO driver to connect with the SQL Server database. Create a PHP script
createSQLServerTable_PDO.php. Obtain a connection with the SQL Server database using the PDO constructor and the
mssql driver for SQL Server. The hostname and
port are specified in
host parameter in format
$connection=new PDO ('mssql:host=localhost,2107;dbname=tempdb', $user, $password);
The procedure to create a database table, add data to the database table, and retrieve data is the same as for the other databases.
In this article you learned to connect to different databases with the PHP PDO drivers using the same set of PHP functions. The same PHP scripts may be used by just modifying the connection parameters.®