user3017335
user3017335

Reputation: 285

Connect to Oracle using PHP

I am trying to connect to Oracle database which is not on my PC. I have installed Xampp, PHP and Oracle instant Client. I have added a windows environmental path to C:\instantclient_11_1. The Oci8 is enabled too when I check from phpinfo(). I have added the extension extension_dir = C:\php-5.4.0\ext to the php.ini and also enabled extension=php_oci8_11g.dll. Then when I try to connect to the database using the code below:

 <?php
 $conn = oci_connect('username', 'password');
 $query = 'select table_name from user_tables';
  ?>

It says Warning: oci_connect(): ORA-12560: TNS:protocol adapter error. Could anyone help?

Upvotes: 0

Views: 3466

Answers (1)

Alex Poole
Alex Poole

Reputation: 191235

The oci_connect call has an optional third parameter for the connection_string. As you aren't specifying TWO_TASK or LOCAL in your environment, as mentioned in the documentation, you have to provide that connection string so that PHP knows how to find and connect to your database.

The general pattern for easy connect syntax is:

$conn = oci_connect('username', 'password', '//hostname:port/service_name');

Where hostname is the name or IP address of the server the database is on - which will not be localhost since you said it is not on your PC; port is the listener port, which defaults to 1521 but could be something else (you can do lsnrctl status on the server to check); and service_name is the database service name, which may or may not be the same as the SID (you can do lsnrctl services to see the valid values on your server).

If you have an SQL Developer connection to the same database, the hostname and port will be shown in the connection settings. It may also show the service name, or may show the SID depending on how it was configured. The service name and SID might be the same. (If you can't run lsnrctl but have sufficient privileges you can try select value from v$parameter where name = 'service_names', but it's likely you won't be able to see that view).

Upvotes: 3

Related Questions