Tag Archives: odbc

Execute SQL remotely on an IBM i via PHP’s PDO ODBC from a Windows or Linux

You’ll first have to configure an ODBC connection “System DSN”/”System Data Source” via the “iSeries Access ODBC driver” or it might be call the IBM i Access driver which you can learn how to download and install here:


Here’s some of the ways to configure the odbc connection


Once you have the ODBC connection setup you can use it in PHP and here’s a Simple Example on how to execute SQL remotely and safely on an IBM i via ODBC.  Note you won’t want to dump the error info in production its there to alert you about errors.

Here’s an example on how to run a SQL extenal stored procedure


Make sure to enable SSL to encrypt your SQL statements that are sent



Run C:\Windows\SysWOW64\odbcad32.exe and edit connection options and make sure ssl is selected or IBM i navigator is defaulted to sslodbc windows secure.png


Setup stunnel

guide is here: under 2.6 Encrypt the connection with Secure Sockets Layer

Other References:
Windows: http://www-03.ibm.com/systems/power/software/i/access/windows.html
Linux: http://www-03.ibm.com/systems/power/software/i/access/linux.html

Setting a default SQL library and library list on a ODBC connection for a IBMi DB2 connection in Windows and Linux

UPDATE: In a JDBC connection using JTOpen 400, library lists are only used when the connection properties is set to naming=system;libraries=MYLIB1,MYLIB AND there’s no default “Schema=” or default schema in the Database URL (i.e. jdbc:as400:MYIBMI/DEFAULTSCHEMA). I’m unsure if this is the same way in ODBC.

ODBC allows you to set a Default SQL Schema or Library and a Library list. This allows you to point your application at different IBMi DB2 libraries.  This could be used so you can have a development, qa, and production libraries and have your application change what data it uses based on your odbc settings.  This also allows you to not need to fully qualify your table names such as SELECT * FROM MYLIB.MYTABLE would be come just SELECT * FROM MYTABLE .  They way this happens is  the default library will by tried first {DefaultLibrary}.MYTABLE and if it doesn’t find it in there it will try the libraries in the library list {library_list…}.MYTABLE [MYLIB1.MYTABLE, MYLIB2.MYTABLE …] .

Below is how to set this up in Windows and in Linux environments.  Make sure you’ve done the steps in this guide to get the odbc driver: https://godzillai5.wordpress.com/2016/09/02/execute-sql-remotely-on-an-ibm-i-via-phps-pdo-odbc-from-a-windows-linux-or-mac/ (mainly this guide http://www-01.ibm.com/support/docview.wss?uid=nas8N1010355)

Windows (ODBC Administration)

Its under the server tab of ODBC Administration tools in windows.  This tool can be found at

C:\Windows\System32\odbcad32.exe – 32 bit
C:\Windows\SysWOW64\odbcad32.exe – 64 bit

You then select your data-source name, configure the data-source, go to the server tab, enter your Default SQL Schema or Library, and the Library list for this connection.


Linux (unixODBC Manager)

edit the /etc/odbc.ini. 

1) add your default library to DefaultPkgLibrary

2) add your library list to DefaultLibraries 

Description = iSeries Access ODBC Driver DSN for iSeries
Driver = iSeries Access ODBC Driver
System = iSeriesSystemName
UserID =
Password =
Naming = 0
DefaultLibraries = MYLIB,MYLIB2,MYLIB3
Database =
ConnectionType = 0
CommitMode = 2
ExtendedDynamic = 0
DefaultPkgLibrary = MYLIB
DefaultPackage = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression = 1
LibraryView = 0
AllowUnsupportedChar = 0
ForceTranslation = 0
Trace = 0


Below is the JDBC connection string with system naming on and the library list to use.  Default schema can’t be specified or it won’t use the libraries.



1. Change the library list in the job description (JOBD) for the ODBC connection. Do a WRKACTJOB and look for QZDASOINIT jobs handles connections and see what JOBD they are using and change it to include the libraries you need.
2. Call the Stored procedure QCMDEXC to run ADDLIBLE.


This will add the library to the library list entries.

ODBC tracing a windows application to an IBMi

In ODBC Administrator go to Tracing tab and click “Start Tracing Now”

Go to run cmd.exe and run cwbcotrc on /allusers

Run the ODBC application until your down with what you wanted to trace

Go to run cmd.exe and run cwbcotrc off

In ODBC Administrator go to Tracing tab and click “Stop Tracing Now”.

Do a search for SQL.Log on your c:/ drive

Go to run cmd.exe and run cwbsvget
Hit enter to open explorer to the cwbsvget.zip file

In that zip file there’s a lot of ODBC api calls to help you better understand whats going on.  I used notepad++’s find in files search to dig into it.

IBMi V6R1 to V7R1 TR10 Gotcha – ODBC – No Field Names retrieved

The odbc connection is connecting to the IBMi (formerly known as iSeries, As400) and pulling back “physical files”/”table names”, but not pulling back column names.  The errors from UPS World Ship were “The selected import/export map is no longer valid because the column names in the external database have changed.”  “Import data source not valid.”

Ways to try and resolve this issue

  1. Install the latest IBM i Access v7.1 windows client to get the latest ODBC driver.
  2. Install the latest service pack for IBM i Access (SI55797_64a.exe at the time of this writing)
  3. Delete QGPL/QZDAPKG
  4. RCLDBXREF OPTION(*CHECK) if there is a problem try RCLDBXREF OPTION(*FIX). If that doesn’t work try RCLSTG SELECT(*DBXREF) when the system can be put into a restricted state and the system can be down for a bit.


Deleting QGPL/QADAPKG  http://www-01.ibm.com/support/docview.wss?uid=nas8N1015556

RCLDBXREF –  http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_61/cl/rcldbxref.htm

RCLSTG – http://www-01.ibm.com/support/docview.wss?uid=nas8N1010683


PHP: odbc_prepare weird issue with bind parameter on Linux to IBMi DB2 connection

I came across this weird issue using odbc_prepare to prepare a SQL statement with ? in place for the parameters. The query would odbc_prepare “successfully” and the odbc_execute would return “true”, but when i tried to fetch the row nothing came back. I eventually figured out that because my parameter was only 5 characters instead of 6 characters that was the reason the query wasn’t pulling back the records I was expecting.

PHP Code

array_push($queryParameters,  str_pad($date, 6, " ", STR_PAD_LEFT));
$preparedQuery = odbc_prepare($this->connection_id, $queryToPrepare);
$success = odbc_execute($preparedQuery, $parameters);
if (odbc_fetch_row($result))
echo odbc_result($result, "MYFIELD");