Feeds

Spread your database connections with PHP PDO

Less is more

Internet Security Threat Report 2014

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.

First steps

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.

extension=php_pdo.dll

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 ntwdblib.dll to 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; host as localhost, port as 3306 and dbname as test. The connection may be set to be a persistent connection using the PDO::ATTR_PERSISTENT attribute.

$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 exec() function.

$connection->exec($sqlstmt);

To create a MySQL table run the PHP script with the URL: http://localhost/createMySQLTable_PDO.php.

Top 5 reasons to deploy VMware with Tegile

Next page: Oracle 10g

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Sway: Microsoft's new Office app doesn't have an Undo function
Content aggregation, meet the workplace ... oh
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
NetWare sales revive in China thanks to that man Snowden
If it ain't Microsoft, it's in fashion behind the Great Firewall
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
Win a year’s supply of chocolate
There is no techie angle to this competition so we're not going to pretend there is, but everyone loves chocolate so who cares.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.