Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not connect oracle database with PHP through PDO

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]])

like image 732
RicardoBarros Avatar asked Nov 26 '17 21:11

RicardoBarros


People also ask

Can PHP connect to Oracle database?

PHP has OCI8 functions that allow scripts to connect to Oracle databases.

What is PDO connection in PHP?

The PDO represents a connection between PHP and a database server. The PDOStatement represents a prepared statement and, after the statement is executed, an associated result set. The PDOException represents an error raised by PDO.

Which type of databases can PDO connect to?

PDO stands for PHP Data Object. Unlike MySQLi, PDO is only object-oriented and supports a number of different database types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL. Important!

What is PDO PHP extension?

PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).


1 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);
like image 133
timclutton Avatar answered Oct 06 '22 01:10

timclutton