Skip to content
PHP Logo

What are the potential risks of using mysql_real_escape_string for preventing SQL injection in PHP?

Using mysql_real_escape_string for preventing SQL injection in PHP is not recommended because it is deprecated as of PHP 5.5.0 and removed in PHP 7. Instead, it is recommended to use parameterized queries with prepared statements using either PDO or MySQLi. This approach is more secure and reliable in preventing SQL injection attacks.

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