Connecting PHP to Oracle

Recently I had to try and connect to Oracle via PHP and had naively assumed it was about as straight-forward as connecting to something like MySQL or Postgres. It is not.

I'm going to try and outline some potential issues and the steps which would be required to do so. 

An outline of what you'll essentially need is:

  • Oracle PHP driver extension. You typically have two choices:
    • oci8 - Oracle's recommended driver
    • pdo_oci - Experimental, developed by the community and for the most part abandoned and may have bugs
  • Oracle's Instant Client can be found here.

Installing the Instant Client

The first thing you'll likely need to do is get the instant client installed. The link I provided above also includes an RPM package from which you could easily install on systems that support that. If you're using a different package system like Debian or similar you could use a tool such as alien to convert it to .deb format.

Then use a command like

alien -i oracle-instantclient**.*-basic-*******.x86_64.rpm

I simply excluded the version #s, just replace it all with the name of the RPM file but you get the idea.

On a system like Windows it's a little easier. Just download the zip package and add it somewhere.

The next thing you'll need to do is define an environment variable named ORACLE_HOME and point it to the root of your Instant client files. If you're using a tnsnames.ora file you'll need to add that somewhere too and assign the path of that file's directory to the TNS_ADMIN environment variable. The following steps usually require that these variables are set correctly. Also, you may need to set the LD_LIBRARY_PATH too. Set this to the lib folder of your instant client.

Installing the extension

Linux

Depending on your flavor of linux you could likely install this particular extension from a repo. There are a variety of ways to install the extension. You could perhaps use pecl

pecl install oci8

If, during the pecl installation process you get an error regarding missing Instant Client headers then you will need to install the SDK headers as well from the same page.

alien -i oracle-instantclient**.*-devel-*******.x86_64.rpm

If for whatever reason this doesn't work you can try compiling the extension from source. Download the source here.

Then once you've downloaded the tarball you can compile it like this:

tar -xzf oci8-*.tar.gz
cd oci8-*
phpize
./configure --with-oci8=instantclient,/usr/lib/oracle/*/client64/lib
make install

This is merely an example, replace oci8*.tar.gz with the appropriate filename. Also the path of with-oci8 is also dependent on the version of the Instant client you chose to install. Verify this path first.

If you lack the phpize command most likely you're missing the php*-dev package. Find it and install it.

Once successfully installed you'll need to modify php.ini to add this .so file within the list of extensions. The output of the complication should have told you where that .so file is.

There are instances where when attempting to verify oci8 has loaded via running the command

php -m

You may see something like "Unable to locate shared library libnnz18.so" or something along those lines. In this case you will probably need to set the LD_LIBRARY_PATH environment variable and point it to the library folder in your instant client path.

In some versions of apache it may be easier to set these variables within the envvars file with your Apache2 installation.

Windows

This might be a little easier. If you installed your environment via WAMP you may already have the necessary extensions installed  you just need to use your setup's configuration to do so. Both WAMP Server and XAMPP may have something.

Otherwise you can go here and download the dll of the extension. If you don't know which version of PHP you're using is thread safe or not  you can likely find out within the phpinfo() output. It should be somewhere near the top.

PHP info thread safety

 

That should be most of what you need to do. You can find some example OCI8 code here that you can test out to ensure you're able to successfully connect to your instance of Oracle.