What is the significance of using PDO in PHP for database operations?

Using PDO in PHP for database operations is significant because it provides a consistent interface for accessing different types of databases, such as MySQL, PostgreSQL, and SQLite. PDO also helps prevent SQL injection attacks by using prepared statements, which separate the SQL code from the user input. Additionally, PDO allows for better error handling and supports transactions for ensuring data integrity.

// 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);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}