ZetCode

PHP PDO Driver Interface

last modified April 19, 2025

The PDO Driver interface in PHP provides a consistent API for database drivers. It defines methods that database-specific PDO drivers must implement.

Basic Definition

The PDO_Driver interface is the base for all PDO driver implementations. It specifies methods required for database connectivity and operations.

Drivers implementing this interface must provide connection handling, query execution, and transaction support. They translate PDO calls to database-specific operations.

PDO Driver Connection Example

This shows how to connect using a PDO driver with MySQL.

pdo_driver_connect.php
<?php

declare(strict_types=1);

try {
    $dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8mb4';
    $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ];
    
    $pdo = new PDO($dsn, 'username', 'password', $options);
    echo "Connected using PDO driver";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

This establishes a connection using the MySQL PDO driver. The DSN specifies the driver (mysql), host, database, and charset. Options configure error handling and fetch mode.

PDO Driver Query Execution

Demonstrates executing a query through the PDO driver interface.

pdo_driver_query.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $result = $pdo->query("SELECT COUNT(*) FROM users");
    $count = $result->fetchColumn();
    
    echo "Total users: " . $count;
} catch (PDOException $e) {
    echo "Query failed: " . $e->getMessage();
}

This executes a simple count query through the PDO driver. The query method returns a statement object. fetchColumn retrieves a single value from the first column.

PDO Driver Prepared Statement

Shows using prepared statements with the PDO driver interface.

pdo_driver_prepared.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('sqlite:/path/to/database.db');
    $stmt = $pdo->prepare("INSERT INTO products (name, price) VALUES (?, ?)");
    
    $products = [
        ['Laptop', 999.99],
        ['Phone', 699.99],
        ['Tablet', 399.99]
    ];
    
    foreach ($products as $product) {
        $stmt->execute($product);
    }
    
    echo "Products inserted successfully";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This uses SQLite PDO driver with prepared statements. The prepare method creates a statement template. execute runs it with different parameter sets.

PDO Driver Transaction

Demonstrates transaction handling through the PDO driver interface.

pdo_driver_transaction.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('pgsql:host=localhost;dbname=testdb', 'user', 'pass');
    $pdo->beginTransaction();
    
    $pdo->exec("UPDATE accounts SET balance = balance - 50 WHERE id = 1");
    $pdo->exec("UPDATE accounts SET balance = balance + 50 WHERE id = 2");
    
    if (/* some condition */) {
        $pdo->commit();
        echo "Transaction completed";
    } else {
        $pdo->rollBack();
        echo "Transaction rolled back";
    }
} catch (PDOException $e) {
    $pdo->rollBack();
    echo "Transaction failed: " . $e->getMessage();
}

This shows PostgreSQL PDO driver transaction handling. beginTransaction starts the transaction. commit/rollBack finalize or cancel it. All operations become atomic.

PDO Driver Fetch Modes

Illustrates different fetch modes available through PDO drivers.

pdo_driver_fetch.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('oci:dbname=//localhost:1521/mydb', 'user', 'pass');
    
    // Fetch as object
    $stmt = $pdo->query("SELECT * FROM employees");
    $stmt->setFetchMode(PDO::FETCH_OBJ);
    
    while ($employee = $stmt->fetch()) {
        echo $employee->name . "\n";
    }
    
    // Fetch into custom class
    class Employee {
        public $id;
        public $name;
        public $department;
    }
    
    $stmt = $pdo->query("SELECT * FROM employees");
    $employees = $stmt->fetchAll(PDO::FETCH_CLASS, 'Employee');
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This demonstrates Oracle PDO driver fetch modes. FETCH_OBJ returns stdClass objects. FETCH_CLASS maps results to custom class instances. Different drivers support the same consistent interface.

PDO Driver Error Handling

Shows error handling techniques with PDO drivers.

pdo_driver_error.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // This will throw an exception
    $pdo->query("SELECT * FROM non_existent_table");
} catch (PDOException $e) {
    echo "Error Code: " . $e->getCode() . "\n";
    echo "Error Message: " . $e->getMessage() . "\n";
    echo "Driver-specific Error Code: " . $e->errorInfo[1] . "\n";
    echo "Driver-specific Error Message: " . $e->errorInfo[2] . "\n";
}

This demonstrates comprehensive error handling with MySQL PDO driver. The exception provides both PDO and driver-specific error information. errorInfo array contains detailed error data from the driver.

PDO Driver Metadata

Shows how to retrieve database metadata through PDO drivers.

pdo_driver_metadata.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
    
    // Get driver name
    echo "Driver: " . $pdo->getAttribute(PDO::ATTR_DRIVER_NAME) . "\n";
    
    // Get server version
    echo "Server version: " . $pdo->getAttribute(PDO::ATTR_SERVER_VERSION) . "\n";
    
    // List tables
    $tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
    echo "Tables:\n";
    print_r($tables);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This retrieves metadata using MySQL PDO driver. getAttribute accesses driver-specific attributes. The query shows database-specific metadata commands while using the standard PDO interface.

Best Practices

Source

PHP PDO Documentation

This tutorial covered the PDO Driver interface with examples showing how different database drivers implement the same consistent API.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP PDO Functions.