Connecting to Oracle via php

December 2016

Oracle database is a useful database server which can be connected through a PHP script. Web servers using SQLPlus can access the Oracle server through this process. Servers based on the Oracle 8i module can normally be connected. For PHP to interact with the Oracle database, the PHP scripting language must be assembled to this extension module. On the Windows OS, the php.ini file must be edited before the process is accomplished.


Intro


Below is an article based on an example of connection to an Oracle database through a php script. However it is not designed to configure your oracle server and oracle client. It ensures that you can access your oracle server from the web server using the SQLPlus.
We can normally connect to a server based on Oracle 8i.

Requirements


It is essential to assemble PHP with Oracle8i module. Under windows, it is crucial to the php.ini to include the line extension=php_oci8.dll

Example of code


$c1 = ocilogon("scott", "tiger", $db);      
$c2 = ocilogon("scott", "tiger", $db);      

function create_table($conn)       

  $stmt = ociparse($conn, "create table scott.hallo (test varchar2(64))");      
  ociexecute($stmt);      
  echo $conn . " created table\n\n";      

function drop_table($conn)       

  $stmt = ociparse($conn, "drop table scott.hallo");      
  ociexecute($stmt);      
  echo $conn . " dropped table\n\n";      


function insert_data($conn)       

  $stmt = ociparse($conn, "insert into scott.hallo       
           values('$conn' || ' ' || to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");      
  ociexecute($stmt, OCI_DEFAULT);      
  echo $conn . " inserted hallo\n\n";



This is one of the easiest way to access Oracle amongst others.

Related :

This document entitled « Connecting to Oracle via php » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.