John Davidson

Connect to multiple versions of Oracle DB with PHP PDO_OCI

0 comments
Message:


i need a ubuntu 20.04 server with php 7.4 to connect multiple version of oracle databases.
i installed the oracle instantclient in version 18.3 and load the oci8 php module.
for new version of oracle databases this works great. for older the connection failed.


these instantclient version is installed:


ii  oracle-instantclient18.3-basic       18.3.0.0.0-4                               amd64        Oracle Instant Client Basic package
ii oracle-instantclient18.3-devel 18.3.0.0.0-4 amd64 Development headers for Instant Client.
ii oracle-instantclient18.3-jdbc 18.3.0.0.0-4 amd64 Supplemental JDBC features for the Oracle Instant Client
ii oracle-instantclient18.3-odbc 18.3.0.0.0-4 amd64 Oracle Instant Client ODBC

this is the php version:


PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS 

basically this version have to be supported:


oracle db 9.2.0.6.0
oracle db 9-0.1.3.1
oracle db 11g 11.2.0.1.0
oracle db 12c 12.2.0.1.0
oracle db 19c 12.2.0.3
oracle db 19c 19.0.0.0.0

this is the current code:


<?php
$dbstr ="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =<SERVERNAME>)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = <SRVNAME>)))";

if(!@($conn = oci_connect('<user>','<pass>',$dbstr)))
{
print_r(ocierror());
die("Connect Error");
}

?>

is it possible? i need some hint how to realize this



My Answer:
To connect to multiple versions of Oracle DB with PHP PDO_OCI, you can use the following code snippet:

php
// Connection parameters
$tns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = your_host)(PORT = your_port))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = your_service_name)))";
$username = "your_username";
$password = "your_password";

// Connect to Oracle DB
try {
$conn = new PDO("oci:dbname=" . $tns, $username, $password);
echo "Connected to Oracle DB successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>


Make sure to replace `your_host`, `your_port`, `your_service_name`, `your_username`, and `your_password` with your actual Oracle DB connection details.

You can use this code to connect to Oracle DB versions 9.2.0.6.0, 9.0.1.3.1, 11g 11.2.0.1.0, 12c 12.2.0.1.0, 19c 12.2.0.3, and 19c 19.0.0.0. Just make sure to provide the correct connection parameters for each version.

If you encounter any issues with connecting to older versions of Oracle DB, you may need to install the corresponding Oracle Instant Client version for that specific database version. You can have multiple versions of Oracle Instant Client installed on your server to support different Oracle DB versions.

Rate this post

5 of 5 based on 7300 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us