PHP Oracle Configuration and Connection Example

The aim of this tutorial is to setup PHP so that it works with Oracle OCI (Oracle Caller Interface), and demonstrate a small example of how you connect to an Oracle Database using php. This can be used for as a standalone php script or PHP within Apache. It also demonstrates a simple oracle/PHP connection string, connecting to a database and executing some SQL.

This article assumes that you have Php Installed. You may or may not have Apache Installed.

Versions used in this example
Windows XP SP2N/A
Php OCI 8
Oracle Basic instant
Links to these files can be found here

Configuring Oracle OCI
  • Download and extract the Php OCI 8 extensions -
  • Copy the php_oci8.dll and the php_oci8_11g.dll to your php "ext" directory. This is normally found here C:\Program Files\PHP\ext
  • Download and extract the oracle basic instant client-
  • copy the oci.dll to a directory in your path. To keep things simple you can copy this to the the same C:\Program Files\PHP\ext directory.
  • Next edit your php.ini file. Again this can be found in your php installation. C:\Program Files\PHP\php.ini. If this 'extention' is commented uncomment it. If it is not there, add it
    ; Note that it should be the name of the module only; no directory information
    ; needs to go here.  Specify the location of the extension with the
    ; extension_dir directive above.
    ; Windows Extensions
  • If you are running Apache, you can restart Apache to pick up these changes.

Oracle Connection Example
  • Create the example below and save it. Modify it to reflect your database.
     1. <?php
     2.   $c     = oci_connect('mariner', 'mariner', '//');
     4.     $q         = "select * from VMDEVICE_PATTERNS where name='$devicename'";
     5.     $stmt     = oci_parse($c, $q);
     7.     oci_execute($stmt, OCI_DEFAULT);
     8.     while ($row = oci_fetch_array($stmt, OCI_ASSOC)) {
     9.         echo $row['PATTERN']."\n";
    10.     }
    12.     oci_free_statement($stmt);
    13.     oci_close($c);
    14. ?>
    Hide line numbers
  • Now you can run this in the command line, or call it from your Apache http server.