Why is it recommended to avoid using the mysql_* functions in PHP and switch to PDO instead?

It is recommended to avoid using the mysql_* functions in PHP because they are deprecated and no longer maintained. It is better to switch to PDO (PHP Data Objects) for database operations as it provides a more secure and flexible way to interact with databases. PDO supports multiple database drivers and offers prepared statements to prevent SQL injection attacks.

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

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