Blog Image

How to establish oracle database connection in php

First of all you need to check whether “oci8” support is enabled or not, if not you have to enable that you can check this in your phpinfo file.

Once this is done.

Now in you test.php file

You have to explicitly set the ORACLE_HOME and LD_LIBRARY_PATH path.
This information will be available in you phpinfo file.

The “$dbstr” variable information you can find it from your “tnsnames.ora” file .

Test.php start

—————————————————————————————–

if (!ini_get(‘display_errors’)) {
ini_set(‘display_errors’, 1);
}
PutEnv(“ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/”);
PutEnv(“LD_LIBRARY_PATH=-Wl,-rpath,/usr/lib/oracle/xe/app/oracle/product/10.2.0/server//lib -L/usr/lib/oracle/xe/app/oracle/product/10.2.0/server//lib -lclntsh”);

$dbstr =”(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.XX)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = YOURDATABASENAME)
)
)”;

if(!@($conn = oci_connect(‘DBUSERNAME’,’DBPASSWORD’,$dbstr)))
{
print_r(ocierror());

}
else
{
echo “connected succesfully”;
}

—————————————————————————————–

Thanks



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.