What are the potential security risks associated with SQL injections in PHP and how can they be prevented?
SQL injections in PHP can allow malicious users to execute unauthorized SQL queries, potentially leading to data loss or unauthorized access to sensitive information. To prevent SQL injections, developers should use prepared statements with parameterized queries instead of dynamically building SQL queries using user input.
// Using prepared statements to prevent SQL injections
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();