What are the advantages of using PDO over mysqli in PHP, especially for beginners?

When comparing PDO to mysqli in PHP, PDO offers several advantages, especially for beginners. PDO supports multiple database drivers, making it easier to switch between different databases without changing the code significantly. Additionally, PDO provides a more secure way to prevent SQL injection attacks by using prepared statements. Lastly, PDO is object-oriented, which can make the code cleaner and easier to maintain.

// Example of connecting to a MySQL database using PDO
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}