What are the potential risks of using real_escape_string() with mysql and mysqli interfaces in PHP?

Using real_escape_string() with mysql and mysqli interfaces in PHP can potentially lead to SQL injection vulnerabilities if not used correctly. It is recommended to use prepared statements with placeholders instead, as they provide a safer and more secure way to interact with databases.

// Using prepared statements with placeholders to prevent SQL injection

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

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

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

// Set the parameter values
$username = "example_username";

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

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

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

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