Do not connect oracle database with PHP through PDO

Viewed 6156

I want to connect the oracle Schema database by default that comes in the oracle bd through PDO with php but it throws me this error:

Fatal error: Uncaught PDOException: SQLSTATE[42S02]: pdo_oci_handle_factory: ORA-12154: TNS:could not resolve the connect identifier specified (ext\pdo_oci\oci_driver.c:640) in C:\xampp\htdocs\ORACLE\52conexion3.php:9 Stack trace: #0 C:\xampp\htdocs\ORACLE\52conexion3.php(9): PDO->__construct('oci:host=localh...', 'hr', 'hr') #1 {main} thrown in C:\xampp\htdocs\ORACLE\52conexion3.php on line 9

    <!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<?php

    $base = new PDO('oci:host=localhost/XE; dbname= Schema - HR', 'hr','hr');

?>
</body>
</html>

In the sql developer in the connection where is the bd of "shema" I gave a right click to see properties and that the data I entered is correct, it says Connection name: Schema - HR, user: hr, password: hr, alias of the network: XE .

Other attempts and still not working:

$base = new PDO('oci:host=localhost;dbname=Schema - HR', 'hr','hr');

$base = new PDO("oci:host=localhost;dbname=Schema - HR", "hr","hr");

$base = new PDO("oci:host=localhost/XE;dbname=Schema - HR", "hr","hr");

$base = new PDO("oci:host=XE;dbname=Schema - HR", "hr","hr");

$base = new PDO('oci:host=localhost;dbname="Schema - HR"', 'hr','hr');

I checked the .ini file and it's apparently right "extension = php_pdo_oci.dll".

NOTE: I searched the php manual and when I do not connect with pdo but through the procedure I do it in this way (using oci_connect) and it works correctly:

$ connection = oci_connect ('hr', 'hr', 'localhost / XE');

// oci_connect: resource oci_connect (string $ username, string $ password [, string $ connection_string [, string $ character_set [, int $ session_mode]])

3 Answers

As documented on the PHP manual page, and mentioned in a comment by mario, a PDO DSN for OCI uses dbname, not schema or host, in its definition:

$conn = new PDO('oci:dbname=localhost/XE', $user, $pass);

The localhost/XE format you are using is an 'EZCONNECT' string. The first part defines the host (localhost) and the second part the service (XE).

You could also use a 'regular' connection string (as normally defined in a tnsnames.ora file) instead:

$conn_string = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE)))';
$conn = new PDO('oci:dbname=' . $conn_string, $user, $pass);

If you're using localhost and set TNS, you can use just a connection alias, in my case orcl:

$conn = new PDO('oci:dbname=orcl', $user, $pass);

On remote, you can:

$conn = new PDO('oci:dbname=hostname', $user, $pass);
Related