What are the potential security risks associated with using the MySQL extension in PHP for database operations?

Potential security risks associated with using the MySQL extension in PHP for database operations include SQL injection attacks, as the extension does not provide built-in protection against them. To mitigate this risk, it is recommended to use parameterized queries with prepared statements when interacting with the database.

// Connect to MySQL database using PDO
$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) {
    echo 'Connection failed: ' . $e->getMessage();
}

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();