Current Location: Home> Latest Articles> PHP Tutorial: Efficiently Connecting to Oracle Database Using PDO and Practical Guide

PHP Tutorial: Efficiently Connecting to Oracle Database Using PDO and Practical Guide

M66 2025-06-15

Overview

PDO (PHP Data Objects) is a PHP extension for database access, offering a unified interface to work with various database types. This article explains how to use PDO to connect to an Oracle database and demonstrates common database operation methods.

Steps

Install Oracle Database Driver Extension

Before connecting to an Oracle database with PDO, ensure that the appropriate Oracle driver extensions are installed and enabled.
  • On Windows, you can enable the drivers by editing the php.ini file and uncommenting the following lines:

;extension=php_pdo_oci.dll
;extension=php_oci8.dll

Then restart your Apache or Nginx server to apply the changes.

  • On Linux, installation is typically done via PECL commands or manual compilation of OCI8 or PDO_OCI extensions. The specific steps vary depending on your operating system and PHP version. Refer to the official PHP documentation for guidance.

Create Database Connection

In your PHP script, create a PDO instance with the following code to connect to the Oracle database:
$dsn = 'oci:dbname=//hostname:port/oracle_sid';
$username = 'your_username';
$password = 'your_password';

try {
    $conn = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}

Here, $dsn contains the Oracle host, port, and SID information. $username and $password are the credentials needed to connect.

Execute SQL Queries

Once connected, use the PDO object to execute SQL statements. The following example queries the employees table and outputs the results:
$sql = 'SELECT * FROM employees';
$stmt = $conn->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
    echo $row['employee_id'] . ' ' . $row['first_name'] . ' ' . $row['last_name'] . '<br>';
}

In this code, $stmt represents the result set. fetchAll retrieves all rows as an associative array, which are then output one by one in a loop.

Use Prepared Statements

PDO supports prepared statements, which improve performance and protect against SQL injection. Here’s an example querying employees by department ID using a prepared statement:
$sql = 'SELECT * FROM employees WHERE department_id = :dept_id';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':dept_id', $dept_id);
$dept_id = 1;
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
    echo $row['employee_id'] . ' ' . $row['first_name'] . ' ' . $row['last_name'] . '<br>';
}

The placeholder ":dept_id" is used for the condition. bindParam binds the actual variable, and then execute runs the query.

Summary

This article explained how to connect to an Oracle database using PDO in PHP and demonstrated basic SQL querying and prepared statement usage. Mastering these skills will improve your database operation efficiency and security, laying a solid foundation for PHP application development.