Connecting PHP to An Oracle Instance on RedHat or CentOS 5

Here lately it seems that everyone wants to connect to Oracle, but I have to admit this was the first time someone asked me to get PHP to talk to Oracle. It was a lot less painful then I thought it would be, so here is what I did.

A long with the standard PHP RPMs you need to install a couple of additional RPMs from Oracle. These are oracle-instantclient-basic and oracle-instantclient-devel which can be downloaded from You will also need php-oci8 RPM which can be download from

So after you have downloaded the RPMs go a head and install the packages and create the symlink for

$ rpm -Uvh oracle-instantclient-basic-##.#.#.rpm
$ rpm -Uvh oracle-instantclient-devel-##.#.#.rpm
$ cd /usr/include/oracle/##.#/[client|client64]
$ ln –s

Now you are going to want to setup you environment settings, It is important to set all Oracle environment variables before starting Apache or running a PHP script,  so that the OCI8 process environment is correctly initialized. Setting environment variables in PHP scripts can lead to obvious or non-obvious problems. You can also add Instant Client library path to /etc/

$ LD_LIBRARY_PATH=/usr/lib/oracle/##.#/[client|client64]/lib:${LD_LIBRARY_PATH}

And now for the big finish. Here is a simple connection script to test it all out.

$c = oci_connect( ‘USERNAME’,

if( $c ) {

$s = oci_parse( $c, ‘SELECT TABLE_NAME FROM all_tables’ );

oci_execute($s) ;

while($res = oci_fetch_array( $s, OCI_ASSOC) ) {

echo $res[‘TABLE_NAME’] . “\n”;


For a complete list of function and additional install resources check out the following sites:

Leave a Reply