What are the alternatives to using the outdated mysql functions in PHP scripts for database queries?

The outdated mysql functions in PHP scripts for database queries are no longer recommended due to security vulnerabilities and lack of support. The alternatives to using these functions are to switch to either MySQLi (MySQL Improved) or PDO (PHP Data Objects) extensions, which provide more secure and flexible ways to interact with databases in PHP.

// Using MySQLi extension as an alternative to outdated mysql functions
$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()) {
        // Process each row
    }
} else {
    echo "0 results";
}

$mysqli->close();