Why is it important to consider switching from mysql_real_escape_string to PDO and Prepared Statements, especially with the deprecation of the Mysql-API in PHP 5.5.0?

Switching from mysql_real_escape_string to PDO and Prepared Statements is important because mysql_real_escape_string is prone to SQL injection attacks and is no longer recommended for use due to the deprecation of the Mysql-API in PHP 5.5.0. PDO and Prepared Statements provide a more secure and reliable way to interact with databases in PHP.

// Using PDO and Prepared Statements to prevent SQL injection

// Establish database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare SQL statement with placeholders
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind parameters
$stmt->bindParam(':username', $username);

// Execute the statement
$stmt->execute();

// Fetch results
$results = $stmt->fetchAll();