How does using prepared statements in PHP affect the need for mysql_real_escape_string?

Using prepared statements in PHP eliminates the need for manually escaping input data using functions like mysql_real_escape_string. Prepared statements automatically handle escaping of input parameters, making the code more secure against SQL injection attacks. This approach separates the SQL query from the data, reducing the risk of injection vulnerabilities.

// Using prepared statements to execute a query securely
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();