Why am I getting this error and how do I fix it?
-
28-10-2019 - |
Question
I have written a PHP script to pull a query from an Oracle database and write results to a csv file. I am using Zend Framework to get connected to the database. I know the credentials are correct as I am able to connect through terminal using SQLPlus.
This is the error:
Zend_Db_Adapter_Exception: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor (/home/http/php-5.3.3/ext/pdo_oci/oci_driver.c:579) in /opt/http/Zend/ZendFramework-1.10.3/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144
I have tried to do research, but no solid solutions. I did read that it could be associated with the tnsnames.ora but I couldn't find that file on the server. I have used Zend before in other projects without any issues.
FYI: I do not have root, so I can't do much magic on the server.
What are your suggestions?
Solution
That's a common errormessage from Oracle, there is nothing to do with zend or even php. I usually get this message in SQLDeveloper or Toad. Check the tnsnames.ora, listener.ora, sqlnet.ora, log files etc. and try to connect with a client app. (if it isn't working, you can report it to the admins)
OTHER TIPS
You need to create a file called tnsnames.ora in your $ORACLE_HOME/network/admin
folder and put in it your SID .
or if you can't create your tnsnames.ora, try setting your DB config using this syntax :
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'oracle',
'params' => array(
'dbname'=> '(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname.domain.tld)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = service_name)))',
'username' => 'dev',
'password' => 'pwd')
)
)
);
$db = Zend_Db::factory($config->database);
Zend_Db_Table::setDefaultAdapter($db);
Althought its better to create tnsnames.ora with your DB config.
For those not using Zend Framework, but having similar issue, a solution to this problem can be found on http://docs.php.net/manual/en/ref.pdo-oci.php#64756
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = yourip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
";
$db_username = "youname";
$db_password = "yourpassword";
try{
$conn = new PDO("oci:dbname=".$tns,$db_username,$db_password);
}catch(PDOException $e){
echo ($e->getMessage());
}
If you're using SID to connect, change
(SERVICE_NAME = orcl)
to
(SID = yourSid)