What are the potential risks of not using prepared statements or mysqli_real_escape_string in PHP when interacting with a MySQL database?

When not using prepared statements or mysqli_real_escape_string in PHP when interacting with a MySQL database, the application is vulnerable to SQL injection attacks. This can lead to unauthorized access to data, data manipulation, and potentially the entire database compromise. To prevent this, it is crucial to use prepared statements or mysqli_real_escape_string to properly sanitize user input before executing SQL queries.

// Using prepared statements to prevent SQL injection
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$stmt->execute();
$result = $stmt->get_result();