How do I use a PHP ODBC extension that's more recent than my version of PHP?

When testing PHP with Easysoft's SQL Server ODBC driver, we found that a later version of PHP's PDO ODBC extension was more reliable than the one that came with the test machine's Linux distribution.

The version of PDO ODBC on the PHP git repository contains important changes for 64-bit platforms. Another benefit for users on 64-bit platforms is that they can build PDO ODBC against a version of unixODBC that is included with their ODBC driver, ensuring that extension, Driver Manager and ODBC driver have the same size SQLLEN types.

To minimise disruption to our system, we wanted to retain our existing version of PHP and only upgrade PDO ODBC, which on our machine was one shared object. However, our attempt to do this failed because there is a mechanism in PHP that prevents the use of an extension that has been compiled against a different version of PHP. We got this error message:

PHP Warning:  PHP Startup: PDO_ODBC: Unable to initialize module
Module compiled with module API=20131227
PHP compiled with module API=20121212
These options need to match

To work around this, we:

  1. Found out our PHP version:
    $ php --version
    PHP 5.5.9-1ubuntu4.3 (cli)

    You can also use the phpinfo() function to find out the PHP version.

  2. Downloaded PHP 5.5.9 from the PHP archive.
  3. Cloned the PHP git repository:
    $ git clone https://github.com/php/php-src.git
  4. Replaced the version of the PDO ODBC source code that was included in PHP 5.5.9 with the one from the git repository:
    $ gunzip php-5.5.9.tar.gz
    $ tar -xvf php-5.5.9.tar
    $ cd php-5.5.9/ext
    $ mv pdo_odbc pdo_odbc_5_5_9
    $ mv ../../php-src/ext/pdo_odbc .
  5. Built PHP 5.5.9/PDO ODBC:
    $ cd ..
    ./configure --with-pdo-odbc=shared,unixODBC,/usr/local/easysoft/unixODBC
    $ make
  6. Copied the PDO ODBC shared object to the system location for PHP extensions:
    $ cd /usr/lib/php5/20121212
    $ sudo mv pdo_odbc.so pdo_odbc.so_5_5_9
    $ cd -
    $ sudo cp modules/pdo_odbc.so /usr/lib/php5/20121212