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

The deprecated mysql_ functions in PHP for database interactions have been replaced by the mysqli_ functions or PDO (PHP Data Objects). It is recommended to use mysqli_ functions or PDO for database interactions to ensure compatibility with newer PHP versions and to prevent security vulnerabilities.

// Using mysqli functions
$mysqli = new mysqli("localhost", "username", "password", "database");

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

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

$mysqli->close();