What are some potential security risks associated with the use of the mysql_* extension in PHP, and how can they be mitigated?

Potential security risks associated with the use of the mysql_* extension in PHP include SQL injection vulnerabilities and deprecated functionality. To mitigate these risks, it is recommended to switch to mysqli or PDO for database operations.

// Example of using mysqli for database operations
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

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

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

while ($row = $result->fetch_assoc()) {
    // Process results
}

$stmt->close();
$mysqli->close();