8
votes

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
As a guess - it does not like the spaces in the database name. - tereško
I tried with other database (with name without spaces "prueba" but also doesn´t work :/ - RicardoBarros
What is this "localhost/XE" syntax? host should be the name of the host, which should be a (DNS) resolvable name; it shouldn't contain slashes. - elixenide
I saw in a forum that they put it that way, but I tried in many ways (see the end of the post). - RicardoBarros
Not sure if the PDO Oracle driver handles/splits the hostname specifically: github.com/php/php-src/blob/master/ext/pdo_oci/… - But the manual says it belongs in the dbname= param: php.net/manual/en/ref.pdo-oci.connection.php - mario

3 Answers

6
votes

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);
0
votes

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);
-1
votes

in your php folder there is php.ini file open it in notepad and remove the extensions before statements who have oci in front then start server again and then run your code.

for connectivity you need to add:

header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Headers: Origin, Content-Type");

 $rest_json = file_get_contents("php://input");
 $_POST = json_decode($rest_json,true);

 $opt   = array(PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                    PDO::ATTR_EMULATE_PREPARES   => false,
                       );
   // Create a PDO instance (connect to the database)
   $pdo     = new PDO('oci:dbname=127.0.0.1/xe;charset=CL8MSWIN1251', 'username', 'password');