What are common pitfalls to avoid when using PHP for database connections and user authentication?

One common pitfall is not using prepared statements when interacting with a database, leaving the application vulnerable to SQL injection attacks. To avoid this, always use prepared statements with parameterized queries to securely interact with the database.

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