What are the recommended alternatives to the deprecated mysql_* functions for database interactions in PHP?

The recommended alternatives to the deprecated mysql_* functions for database interactions in PHP are to use either MySQLi (MySQL Improved) or PDO (PHP Data Objects) extensions. These extensions provide more secure and flexible ways to interact with databases, as well as support for prepared statements to prevent SQL injection attacks.

// Using MySQLi extension
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

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

$query = "SELECT * FROM table";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
    }
} else {
    echo "0 results";
}

$mysqli->close();