What potential security risks are associated with using the mysql_ extension in PHP and how can they be mitigated?

Using the mysql_ extension in PHP poses security risks such as SQL injection vulnerabilities and lack of support for modern MySQL features. To mitigate these risks, it is recommended to switch to mysqli or PDO extensions, which provide prepared statements and parameterized queries to prevent SQL injection.

// 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);
}