How to connect to an Oracle database from PHP

Updated on 11th October 2017

The recommended method for connecting PHP applications to Oracle Databases is using Zend Sever. The Zend Server is an Oracle enabled enterprise ready web application server for running and managing PHP applications on both Linux and Windows platforms. However if you do not wish to use Zend Server, this article shows you how to manually install and configure PHP OCI8 extension and Oracle Instant Client Libraries to connect PHP applications with Oracle.

Install Instant Client on Linux

Download the instant client zip file or RPM from the OTN Instant Client page. All installations require the Basic or Basic Light RPM.

Install instant client with RPM

  1. Install the RPMs as the root user, using yum.
    # yum install oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm
    
  2. Add the directory for the Instant Client to the environment variable LD_LIBRARY_PATH, for example:
    # export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client/lib:$LD_LIBRARY_PATH
    
  3. Add the bin directory of Instant Client to the PATH environment variable, for example:
    # export PATH=/usr/lib/oracle/12.2/client/bin:$PATH
    

Install Instant Client with ZIP

  1. Download the Basic or Basic Light ZIP package.
  2. Unzip the packages into a single directory such as /opt/oracle/instantclient_12_2
    # cd /opt/oracle
    # unzip instantclient-basic-linux-12.2.0.1.0.zip
    
  3. Create the appropriate libclntsh.so and libocci.so links for the version of Instant Client. For example
    # ln -s libclntsh.so.12.1 libclntsh.so
    # ln -s libocci.so.12.1 libocci.so
    
  4. Install the libaio package (also called libaio1 in some distros), for example on Oracle Linux, run this as the root user:
    # yum install libaio
    
  5. Set the environment variable LD_LIBRARY_PATH to the directory where you unzipped the instant client, for example:
    # export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2:$LD_LIBRARY_PATH
    
  6. Add the bin directory of Instant Client to the PATH environment variable, for example:
    # export PATH=/opt/oracle/instantclient_12_2:$PATH
    

Install PHP OCI8

You can download and install the latest version of OCI8 with PECL.

To install for PHP 7:

# pecl install oci8

To install for PHP 5.2 - PHP 5.6

# pecl install oci8-2.0.12

PECL is a repository of PHP extensions written in C and it comes along with PEAR (PHP Extensions and Application Repository). Follow the steps below to install PECL if you don't already have it.

On Linux and UNIX :

# wget http://pear.php.net/go-pear.phar
# php go-pear.phar

On Windows, you can copy the script at this location http://pear.php.net/go-pear.phar to a local file named go-pear.phar and run

c:\php> php go-pear.phar

Enable OCI8

After installation the OCI8 shared extension oci8.so library will be available in the extension directory. To enable this extension, edit php.ini and add the following line.

 extension=oci8.so

On windows, the extension DLL file will be available in the extension directory. You need to uncomment the below line in php.ini file.

 extension=php_oci8_11g.dll 

Restart Apache

THe final step in the installation is to restart your Apache webserver for the extension to load.

Verifying the PHP-Oracle Connection

  1. Copy the below script to a PHP file, say oraconnect.php
  2. This script connects to the Oracle sample schema HR. Modify the password, service name in oci_connect() match your database.
  3. Save the file in your Apache htdocs folder and load it from a browser.
  4. If the installation is successful your script will display the first and last names of all emmployees from EMPLOYEES table in the Oracle HR schema.
    <?php
    
    $conn = @oci_connect('HR', 'password', 'localhost/ORCL');
    
    if (!$conn) {
       die("Database Connection Error");
    }
    
    $stid = oci_parse($conn, 'SELECT * from EMPLOYEES');
    oci_execute($stid);
    echo "<table>";
    echo "<tr><th>First Name</th><th>Last Name</th></tr>";
    while (($emp = oci_fetch_array($stid, OCI_BOTH)) != false) {
    	echo "<tr>";	
    	echo "<td>".$emp['FIRST_NAME']."</td>";
    	echo "<td>".$emp['LAST_NAME']."</td>";
    	echo "</tr>";
    	}
    echo "</table>";
    
    ?>
    

Post a comment

Got a question about this article? Ask our forum

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.