In what ways can the use of PDO or mysqli_* be advantageous over the deprecated mysql_* extension for database interactions in PHP?

The use of PDO or mysqli_* over the deprecated mysql_* extension is advantageous because they offer more secure and flexible ways to interact with databases. PDO provides a consistent interface for accessing different types of databases, while mysqli_* offers improved performance and additional features like prepared statements to prevent SQL injection attacks.

// Using PDO to connect to a MySQL database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to the database";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}