Thursday, November 7, 2013

PHP setting for oracle 11g Express Edition Database

After installing Oracle Database 11g Express Edition , you must need to do some PHP setting to connect and used the oracle database.
You need to enable the oci8_11g package for that. For this you may open your php.ini configuration file and find the extension=php_oci8_11g.dll
You may find following block of code like this

extension=php_mysql.dll
extension=php_mysqli.dll
;extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll  ; Use with Oracle 11gR2 Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll

You just need to remove semicolon (;) from the begining of the extension=php_oci8_11g.dll  statement. Then restart all services and check it in your phpinfo.


Now, You can connect the database the sample code is as follows.

<?php

// Create connection to Oracle
$conn = oci_connect("user1", "password",  'localhost');

if (!$conn)
{
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
else
{
   print "Connected to Oracle!";
}

$Rs = oci_parse($conn, 'select * from emp_master');
oci_execute($Rs);
echo "<pre>\n";

while (($row = oci_fetch_assoc($Rs)))
{
   print_r($row) ;
}

// Close the Oracle connection
oci_close($conn);
?>