What are some common pitfalls to avoid when working with PHP and databases, such as SQL injection?

SQL injection is a common security vulnerability where an attacker can manipulate a database query by inserting malicious SQL code. To prevent SQL injection, always use prepared statements with parameterized queries to sanitize user input and avoid directly inserting user input into SQL queries.

// Example of 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', $_POST['username']);
$stmt->execute();
$result = $stmt->fetchAll();