What are the benefits of using PDO over MySQLi for prepared statements in PHP?

Using PDO over MySQLi for prepared statements in PHP offers several benefits, including support for multiple database systems, object-oriented approach, and easier error handling. PDO allows developers to switch between different database systems without changing the code significantly, making it more flexible and scalable.

// Using PDO for prepared statements in PHP
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $id = 1;
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    print_r($result);
} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}