What are common pitfalls to avoid when using PHP to handle and present data from a database in a web application?

One common pitfall to avoid when using PHP to handle and present data from a database in a web application is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries when interacting with the database.

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