What is the potential risk of SQL injection in PHP code?

SQL injection occurs when an attacker inserts malicious SQL code into a query, potentially gaining unauthorized access to the database or manipulating its data. To prevent SQL injection in PHP code, it is essential to use prepared statements with parameterized queries. This approach separates the SQL query logic from the user input, making it impossible for attackers to inject SQL code.

// Using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();