What are the potential security risks associated with using the deprecated mysql extension in PHP?

The deprecated mysql extension in PHP poses security risks such as SQL injection attacks, as it does not support prepared statements or parameterized queries. To mitigate these risks, developers should switch to using mysqli or PDO extensions, which provide better security features and support for modern database interactions.

// Connect to MySQL using mysqli extension
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Perform a query using prepared statements
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = "example";
$stmt->execute();
$result = $stmt->get_result();

// Process the result set
while ($row = $result->fetch_assoc()) {
    echo "Username: " . $row["username"];
}

// Close the connection
$mysqli->close();