What are the advantages of using newer MySQL extensions like MySQLi or PDO over the traditional mysql extension in PHP?

The traditional mysql extension in PHP is deprecated and not recommended for use due to security vulnerabilities and lack of support for newer MySQL features. It is recommended to use newer extensions like MySQLi (MySQL Improved) or PDO (PHP Data Objects) for improved security, performance, and flexibility in database interactions.

// Using MySQLi extension
$mysqli = new mysqli("localhost", "username", "password", "database_name");
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Using PDO extension
try {
    $pdo = new PDO("mysql:host=localhost;dbname=database_name", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}