How to Query IBM i Data with PHP and PDO_ODBC

ODBC & Db2 for iOf the PHP extensions that can connect to IBM i Db2, we recommend PDO_ODBC whenever possible.

Although the classic extensions (ibm_db2, PDO_IBM) and older ODBC continue to work, we prefer PDO_ODBC for several reasons:

  • PDO_ODBC supports many databases and PHP frameworks
  • PDO’s fetchAll() can retrieve all rows at once, to fetch small recordsets quickly
  • The PHP Toolkit can connect over PDO_ODBC
  • Supports stored procedure output parameters better than other extensions

Example script using PDO_ODBC to query the sample QCUSTCDT table

We included try/catch blocks to demonstrate good error handling.

 

Note about special characters

If the User ID or Password contains a semicolon (;), wrap the value in curly braces:

More about PDO_ODBC

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.