skip to Main Content

I Need your help for connect Oracle in PHP. Using Red hat server 8.9 , PHP version is 8, OCI8 is enabled, HTTPD web server installed.
Whatever steps required i did like,

> /etc/httpd/conf/httpd.conf

SetEnv LD_LIBRARY_PATH /home/oracle/Avani/dbhome_1/lib
SetEnv NLS_LANG American_America.UTF8
SetEnv PATH /usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin
PassEnv LD_LIBRARY_PATH

/etc/sysconfig/httpd

export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export ORACLE_HOME=/home/oracle/Avani/dbhome_1
export ORACLE_BASE=/home/oracle
export PATH=/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin

~/.bashrc
# .bashrc

# User specific aliases and functions



export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export NLS_LANG=American_America.UTF8
export TNS_ADMIN=/home/oracle/Avani/dbhome_1/network/admin
export PATH=$PATH:$ORACLE_HOME/bin:$LD_LIBRARY_PATH:.
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH:$ORACLE_HOME/bin

~/.bash_profile
#.bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

export ORACLE_HOME=/home/oracle/Avani/dbhome_1
export ORACLE_SID=orcl

export LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib
export NLS_LANG=American_America.UTF8
export TNS_ADMIN=/home/oracle/Avani/dbhome_1/network/admin
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH:$ORACLE_HOME/bin:$PATH:.
# User specific environment and startup programs



export PATH

OCI8 is enabled

Error

Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that LD_LIBRARY_PATH includes the directory with Oracle Instant Client libraries in /var/www/html/db2.php on line 10

Warning: oci_connect(): Error while trying to retrieve text for error ORA-01804 in /var/www/html/db2.php on line 10

Warning: Trying to access array offset on false in /var/www/html/db2.php on line 14
Connection failed:

Code

<?php
// Enable error reporting in PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Connect to the Oracle database using oci_connect
$conn = oci_connect("toakdbi", "toakdbi_123", "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=AvaniServer)(PORT=1521)))(CONNE_DATA=(SID=orcl)))");

if (!$conn) {
    $e = oci_error();
    echo "Connection failed: " . $e['message'];
} else {
    echo "</br>Connected to Oracle!";
}
?>

/etc/systemd/system/httpd.service.d/httpd.conf

[Service]
    Environment="LD_LIBRARY_PATH=/home/oracle/Avani/dbhome_1/lib"
Environment="PATH=/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/oracle/Avani/dbhome_1/bin"

2

Answers


  1. Never set ORACLE_HOME when using Oracle Instant Client. Also, with Instant Client, setting ORACLE_SID doesn’t make sense. So unset these. Try using the ldconfig command as mentioned in the installation doc instead of setting LD_LIBRARY_PATH.

    Your path /home/oracle/Avani/dbhome_1/lib/instantclient looks suspicious. If you have installed the database, then you don’t need to install Instant Client. The DB /lib directory already has the correct libraries.

    Overall I recommend installing the Instant Client RPM instead of the Instant Client ZIP. The RPM will set the library search path correctly as part of its install. Then you can unset all the Oracle environment variables. Passing the environment variables through Apache gets tricky since you need to put them in the correct file. Setting the variables in your shell is generally not sufficient and won’t work

    Login or Signup to reply.
  2. You have a small misspelling in your code, it should be CONNECT_DATA instead of CONNE_DATA.

    // Connect to the Oracle database using oci_connect
    $conn = oci_connect("toakdbi", "toakdbi_123", "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=AvaniServer)(PORT=1521)))(CONNECT_DATA=(SID=orcl)))");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search