Tuesday, May 1, 2018

PHP Oracle Database connection in Xamp

If you installed xamp for php programing and want to connect Oracle database as backend then you have to enable this. By default we did not get php oracle enabled.
PHP has OCI8 extention, which provide oracle connectivity to php application. OCI8 uses "oracle Instant Client" Package to get oracle specific function.

1. Download the "Instant client Package-Basic" for windows  from the OTN instant client page.



2. Unzip it to c:\instantClient_11_2

3. Now need to edit  System "Environment Variables". For this go to My computer property click on "Advance System Setting" and then Environment Variables. and add "C:\instantclient_11_2" to system variable PATH.


4. Restart Your computer.

5. Now find the below string in php.ini file.
;extension=php_oci8.dll
;extension=php_oci8_11.dll

remove semicolon( ;) to uncomment string.

6. Now Download the Actual Thread safe OCI-DLL from  https://pecl.php.net/package/oci8/2.0.8/windows
Download according to you php version. suppose your php version is 5.4, then download 5.4 Thread safe(TS) x86 zip file.


7. unzip OCI-DLL zip and and copy all files from this folder to Extention Directory of PHP C:\xampp\php\ext

8. Restart xamp Appache server.

9. To make sure that the connection to oracle database is successfully activated, go to phpinfo (http://localhost/dashboard/phpinfo.php). find string 'oci8' .If found, then xamp can communicate from Oracle database
Now we check connection using below PHP script :

<?php

$DB ="(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.3)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = SERVICE_NAME)
    )
  )";
$conn=oci_connect('USERID','PASSWORD',$DB);

if(!$conn)
{
die('Could not connect: ' . oci_error());
}
else
{
 echo "Successfully";
}

?>