What are the potential security risks of using the mysql_ API in PHP for database interactions?

Using the mysql_ API in PHP for database interactions can pose security risks such as SQL injection attacks due to its lack of prepared statements and parameterized queries. To mitigate this risk, it is recommended to switch to using PDO or MySQLi extensions which offer prepared statements for safe database interactions.

// Using PDO for secure database interactions
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}