How can the use of deprecated MySQL functions in PHP be replaced with modern alternatives like PDO or MySQLi?

Deprecated MySQL functions in PHP can be replaced with modern alternatives like PDO or MySQLi by rewriting the database queries using the newer functions. PDO and MySQLi offer improved security features and better support for prepared statements, making them more secure and efficient options for interacting with databases in PHP.

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

try {
    $pdo = new PDO($dsn, $username, $password);
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    
    // Fetch results
    while ($row = $stmt->fetch()) {
        // Process results
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}