Category Archives: PHP

Installing iSeries Access ODBC Driver for PHP on Ubuntu

leverage-the-power-of-ibm-i-zend-phpPrerequisites: You’ve got to have PHP5 installed and running; this isn’t part of the tutorial.

Sure, it’s a highly specific title… but I’ve spent days pulling my hair out (and my hair is thinning, so I really can’t spare any) attempting to get Ubuntu to talk to a DB2 Database running on IBM iSeries.

Now, the IBM iSeries is not my machine… I don’t, nor do I want to, know anything about it. I only care about the PHP environment I’m working with.

So here’s how I got it done with a relatively vanilla installation of Ubuntu 12.04 LTS.

Step 1 – Install PHP ODBC and Alien

This is the first of the many (seriously, there aren’t THAT many steps) steps to get it running. Get into BASH and then run

$ sudo apt-get install php5-odbc alien

We’re installing alien because IBM provide a “Linux compatible” version of the iSeries Access ODBC Driver, but it’s only available as a Red Hat/CentOS package… [facepalm]

Step 2 – Download the RPM (whaaaat?)

Yes, an RPM…

Go create yourself an IBM account then visit https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=ial. I used IBM i Access for Linux V7R1, and then the “For Intel-based Linux workstations”… if you choose differently, you’re on your own.

If you’ve made it this far, congratulate yourself on having an attention span longer than a 1 year old child. It’s really nothing to be too satisfied about, so punch yourself in the nose and get back to it!

Step 3 – Convert and install the RPM

You will quite probably get an error that odbcinst is unable to be run, so prior to installing the RPM, go ahead and execute

$ sudo apt-get install odbcinst

Good job! (joking) Now, the highly complex task of converting an RPM to a .deb file and installing it… wait for it!

$ sudo alien -i -c iSeriesAccess-7.x.x-1.0.i386.rpm

NOTE: You need to change out the version numbers with whatever the RPM is called that you downloaded.

We also need to create symbolic links from the default Ubuntu lib directory to point to the (potentially 64-bit) provided by the IBM installation.

sudo ln -s /opt/ibm/iSeriesAccess/lib64/libcwb* /usr/lib

Huzzah! We’re nearly there.

Step 4 – Create the DSN

So, this last step kept me entertained (read: suicidal) for a couple of days. Because I come from a background of “conventional” PHP development (using MySQL/Postgres for the RDBMS) I kept thinking I should just be able to set up my connection with the following style of string

$conn = new PDO('odbc:Driver={iSeries Access ODBC Driver};System=db.domain.com;Database=myDb;Dbq=SOMELIB;Naming=1;', 'username', 'password');

Unfortunately, I got this error

[unixODBC][Driver Manager]Data source name not found, and no default driver specified

Theoretically, this should be possible. But I gave up trying and concluded that attempting to set up a System DSN was probably better. This involved setting up an odbc.ini file for the entire system. This box was our dev machine, and used only for development… connecting to the DEV database… so it makes sense, all users running on this box should be able to use ODBC with a DSN of myDBdev and instantly connect to it.

I set up the /etc/odbc.ini file thusly

[myDBdev]
Description = iSeries Access ODBC Driver DSN for iSeries
Driver = iSeries Access ODBC Driver
System = db.domain.com
UserID = myDbUser
Password = myDbPassword
Naming = 1
DefaultLibraries = SOMELIB
Database = myDb

This should fix all problems and we should be able to down a golden ale in celebration… fire off the php script. In my case I had a test script set up with the contents

// odbc.php
try {
    $conn = new PDO('odbc:myDBdev'); // Note: The name is the same as what's in our square brackets in ODBC.ini
    $stmt = $conn->prepare("SELECT * FROM myTable WHERE id = :id");
    $stmt->execute(array('id' => 123));
    while ($row = $stmt->fetch()) {
        print_r($row);
        echo '
';
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}

Then I simply called the script from the command line with

$ php odbc.php

Then… then… then…

Array(
[ID] => 123,
[0] => 123,
[CURDSC] => My description,
[1] => My description,
[SOMEVAL] => Some value,
[2] => Some value
)

I could have hugged a cactus, I was so happy! All I have to do now is set up the same ODBC connections with different DSN’s (test_db, stage_db, production_db) and all of my deployment systems will be amazeballs.

You’re welcome.

Using PHP from command line on OSX

It’s no secret, OSX is the best operating system for personal computing. The barrage of haters will tell you it’s for soy decafe mocha latte sipping hipsters; but they’re misinformed luddites. Nevermind, we’ll let them live in their world of fantasy and continue powering on with our proper shell, Unix-style system, our anti-aliasing and Helvetica.

By default, OSX comes with PHP pre-installed. However, this probably isn’t the version you’re developing with.

I’m concentrating on a development environment that uses MAMP as its localhost development server. It’s quite popular, and saves a lot of compiling and messing around. However, if you’re using a custom compiled version of PHP with Apache, but your command line shows a different version being used, you can use similar techniques to those described below.

Step 1 – Establish the version of PHP being used by CLi

Jump into your terminal (my favourite being iTerm 2) and fire off the command

$ php --version

You should receive a response along the lines of

PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Step 2 – Determine the version of PHP being used by MAMP

Pretty straight forward, just open up MAMP and hit the “Preferences” button.

MAMP_and_Edit_Post_‹_Josh_Ellis_—_WordPress
Then you’ll see something along these lines.

MAMP-2
If these two match… you’re winning at life. Crack a beer and have a celebratory nap for the afternoon.

Step 3 – Find the path to your PHP binaries

MAMP, by default, will have installed PHP in

/Applications/MAMP/bin/php/php5.X.X/bin

Open finder and make sure of the available PHP versions (based on the preferences above). Once you’ve successfully determined the name of the folder, pop the following into your terminal.

$ sudo nano ~/.bash_profile

Fear not if this file has content in it already. Just use the down arrow and go to the bottom of the file. If the file doesn’t exist, it will be created when we save it on exit. :-)
Add the following line at the bottom of your file

export PATH="/Applications/MAMP/bin/php/php5.X.X/bin:$PATH"

Please ensure you replace the “X.X” with your relevant version! Then hit Ctrl + X… type “Y”, then hit enter. This line prepends your system path with the directory for the PHP binaries. By PREpending, the system starts at the beginning of these arguments (generally separated by a colon “:”) and goes until it finds a suitable match for whatever command is attempting to be executed.
Confirm your file has been saved by typing

$ cat ~/.bash_profile

If something has gone wrong… repeat the last couple of steps.
Following this, we need to make sure the file is executable. This last command should ensure the permissions are correctly set on the file.

$ chmod u+x ~/.bash_profile

The last thing to do is restart your terminal, then fire off

$ echo $PATH

All going well you’ll see your newly prepended directory to your PHP binaries. You can confirm it’s now running the correct version by typing

$ php --version

Your expected output is

PHP 5.4.4 (cli) (built: Jul  4 2012 17:28:56)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v2.0.0, Copyright (c) 2005-2012, by mOo

You’re welcome.