What are the advantages of switching from the mysql_ extension to mysqli or PDO for database operations in PHP?

Switching from the mysql_ extension to mysqli or PDO in PHP for database operations is beneficial for several reasons. 1. Improved security: mysqli and PDO offer prepared statements and parameterized queries, which help prevent SQL injection attacks. 2. Better performance: mysqli and PDO are more efficient in handling database operations, resulting in faster execution of queries. 3. Support for newer MySQL features: mysqli and PDO provide access to advanced features of MySQL, such as stored procedures and transactions.

// Using mysqli for database operations
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Perform database operations using mysqli

$mysqli->close();