What is the potential risk of SQL injection in PHP and how can it be prevented?
SQL injection is a common security vulnerability in PHP applications where attackers can insert malicious SQL statements into input fields, potentially gaining unauthorized access to the database. To prevent SQL injection, developers should use parameterized queries or prepared statements instead of directly inserting user input into SQL queries.
// Using parameterized queries to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();