What potential security risks, such as SQL injection, should be considered when working with SQL queries in PHP?

One potential security risk when working with SQL queries in PHP is SQL injection, where an attacker can manipulate input data to execute malicious SQL statements. To prevent SQL injection, you should always use prepared statements with parameterized queries in PHP. This helps to separate SQL code from user input, preventing attackers from injecting malicious SQL code.

// Using prepared statements to prevent SQL injection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();