What potential issues can arise from using the mysql_ extension in PHP, and what alternatives are recommended?

Using the mysql_ extension in PHP can lead to security vulnerabilities and deprecated functionality. It is recommended to switch to either the mysqli or PDO extensions, which offer better security features and support for prepared statements.

// Using mysqli extension as an alternative to mysql_

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform SQL query
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

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

// Close connection
$conn->close();