What are the potential security risks associated with SQL injection in PHP scripts?

SQL injection in PHP scripts occurs when user input is not properly sanitized before being used in SQL queries, allowing malicious users to manipulate the queries and potentially gain unauthorized access to the database. To prevent this, always use prepared statements with parameterized queries to ensure that user input is treated as data and not executable code.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->execute(['username' => $username, 'password' => $password]);
$user = $stmt->fetch();