Home » Php » oracle – PHP oci_connect() TNS:could not resolve the connect identifier (ORA-12154)

oracle – PHP oci_connect() TNS:could not resolve the connect identifier (ORA-12154)

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m testing some PHP code on Ubuntu server and oracle database connection is checked by the “tnsping” command

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.14)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ORCL) (SID = ORCL)))
OK (10 msec)

But, oci_connect function in PHP shows an warning like as

Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in /usr/local/ipm5/wui/manager/123.bih on line 6 2

of course, the OCI8 is enabled(checked via phpinfo) and some environment(PATH,ORACLE_BASE,ORACLE_HOME,ORACLE_SID,TNS_ADMIN,LD_LIBRARY_PATH ) variables are set into /etc/bash.bashrc

do anyone recommend?? what the problem is.

How to&Answers:

instead of ORCL, you may want to put the whole string in oci_connect

(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.14)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ORCL) (SID = ORCL)))

PHP code:

oci_connect($username, $password, '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.14)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ORCL) (SID = ORCL)))');

check phpinfo() output “Environment” part, if you don’t have ORACLE_HOME, TNS_ADMIN, you need to make those available to your PHP running environment,
for apache compiled with php_module

export ORACLE_HOME=/path/to/oracle_home
export TNS_ADMIN=/path/to/tns_admin
apachectl start

for php-cgi or php-fpm

export ORACLE_HOME=/path/to/oracle_home
export TNS_ADMIN=/path/to/tns_admin
/script/to/start/fpm

Answer:

This can also happen when using a remote database that is unreachable, or requires a VPN connection to be accessed.