What are the potential risks of using mysql_real_escape_string in PHP and how can they be mitigated?

The potential risk of using mysql_real_escape_string in PHP is that it is deprecated and no longer recommended for use due to security vulnerabilities. To mitigate this risk, it is recommended to use parameterized queries with prepared statements using PDO or MySQLi.

// Using prepared statements with PDO
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();