What are the recommended configurations and environment variables that need to be set in PHP and the server for successful connection to an Oracle database using PDO?

To successfully connect to an Oracle database using PDO in PHP, you need to ensure that the necessary configurations and environment variables are set correctly. This includes installing the PDO_OCI extension, setting the Oracle Instant Client library path in the PATH environment variable, and configuring the tnsnames.ora file with the database connection details. Additionally, make sure that the appropriate Oracle database drivers are installed on the server.

<?php
$tns = "
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = your_host)(PORT = your_port))
        )
        (CONNECT_DATA =
            (SERVICE_NAME = your_service_name)
        )
    )
";

try {
    $dbh = new PDO("oci:dbname=" . $tns, 'username', 'password');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>