What are the potential security risks associated with using PHP to access SQL databases, and how can they be mitigated?

One potential security risk associated with using PHP to access SQL databases is SQL injection attacks, where malicious users can manipulate SQL queries to access or modify sensitive data. This risk can be mitigated by using prepared statements and parameterized queries to sanitize user input and prevent unauthorized SQL commands from being executed.

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