What are the potential pitfalls of using the "mysql_real_escape_string" function in PHP for database queries?

The "mysql_real_escape_string" function in PHP is deprecated and should not be used for database queries. Instead, it is recommended to use parameterized queries with prepared statements to prevent SQL injection attacks. This method separates the SQL query logic from the user input, making it safer and more secure.

// Using parameterized queries with prepared statements to prevent SQL injection

// Establish a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a SQL statement with a placeholder for the user input
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");

// Bind the user input to the placeholder
$stmt->bind_param("s", $username);

// Set the user input
$username = $_POST['username'];

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

// Fetch the results
$result = $stmt->get_result();

// Process the results
while ($row = $result->fetch_assoc()) {
    // Do something with the data
}

// Close the statement and the connection
$stmt->close();
$mysqli->close();