Spread your database connections with PHP PDO

Less is more

Choosing a cloud hosting partner with confidence

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 PDO:ATTR_CURSOR attribute.

$stmt ->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL);

Bind a PHP variable for the catalogId value to the parameter marker using the bindParam() function.

$stmt->bindParam(1, $catalogId);

Run the prepared statement using the execute() function.


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.

Oracle 10g

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 localhost,port.

$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.®

Providing a secure and efficient Helpdesk

More from The Register

next story
Preview redux: Microsoft ships new Windows 10 build with 7,000 changes
Latest bleeding-edge bits borrow Action Center from Windows Phone
Google opens Inbox – email for people too thick to handle email
Print this article out and give it to someone tech-y if you get stuck
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Entity Framework goes 'code first' as Microsoft pulls visual design tool
Visual Studio database diagramming's out the window
Google+ goes TITSUP. But WHO knew? How long? Anyone ... Hello ...
Wobbly Gmail, Contacts, Calendar on the other hand ...
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Redmond top man Satya Nadella: 'Microsoft LOVES Linux'
Open-source 'love' fairly runneth over at cloud event
prev story


Choosing cloud Backup services
Demystify how you can address your data protection needs in your small- to medium-sized business and select the best online backup service to meet your needs.
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.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.