How can the SQL query in the PHP script be improved to prevent errors?

The SQL query in the PHP script can be improved to prevent errors by using prepared statements. Prepared statements help prevent SQL injection attacks and ensure that the query is executed safely. By using prepared statements, you can separate the SQL query from the user input, making it more secure.

// Improving the SQL query using prepared statements
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->execute();