What are the advantages of using PDO over ODBC for database operations in PHP?

Using PDO over ODBC for database operations in PHP offers several advantages, including better performance, support for multiple database types, and improved security through prepared statements. PDO is a more modern and flexible database abstraction layer that provides a consistent interface for accessing different databases, making it easier to switch between database systems.

// Connect to a MySQL database using PDO
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo 'Connected to the database';
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}