What are some potential pitfalls of using the mysql_real_escape_string function in PHP for database queries?
Using `mysql_real_escape_string` can be problematic because it is deprecated as of PHP 5.5.0 and removed in PHP 7. It is recommended to use parameterized queries with prepared statements using PDO or MySQLi to prevent SQL injection attacks. This ensures that user input is properly sanitized and escaped before being used in database queries.
// Using PDO with prepared statements to prevent SQL injection
$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();
$results = $stmt->fetchAll();