PHP Oracle Configuration and Connection Example

Aim
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.

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

Versions used in this example
Sofware/ComponentImage
Windows XP SP2N/A
PhpN/A
Php OCI 8 extentionsphp_oci8-1.3.5-5.2-Win32-vc6-x86.zip
Oracle Basic instant Clientinstantclient-basic-win32-11.1.0.7.0.zip
Links to these files can be found here

Configuring Oracle OCI
  • Download and extract the Php OCI 8 extensions - php_oci8-1.3.5-5.2-Win32-vc6-x86.zip
    `
  • 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- instantclient-basic-win32-11.1.0.7.0.zip
    `
  • 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.
    
    extension=php_oci8.dll
    
    
    ; 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', '//mymarinerdb.tmicro.com:2523/MARPRD');
     3.   
     4.     $q         = "select * from VMDEVICE_PATTERNS where name='$devicename'";
     5.     $stmt     = oci_parse($c, $q);
     6. 
     7.     oci_execute($stmt, OCI_DEFAULT);
     8.     while ($row = oci_fetch_array($stmt, OCI_ASSOC)) {
     9.         echo $row['PATTERN']."\n";
    10.     }
    11.         
    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.