In PHP, what are the advantages of using modern database extensions like PDO over the older mysql_ functions for improved security and efficiency?

Using modern database extensions like PDO over the older mysql_ functions provides improved security by allowing the use of prepared statements, which help prevent SQL injection attacks. Additionally, PDO supports multiple database drivers, making it more versatile. It also offers better error handling and is more object-oriented, making code more readable and maintainable.

// Using PDO to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

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();
}