What are common pitfalls to avoid when using PHP to search and retrieve data from a database?

One common pitfall to avoid when using PHP to search and retrieve data from a database is not sanitizing user input, which can leave your application vulnerable to SQL injection attacks. To mitigate this risk, always use prepared statements with parameterized queries to prevent malicious SQL code from being injected into your database queries.

// Example of using prepared statements to search and retrieve data from a database
$search_term = $_GET['search_term'];
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE column_name = :search_term");
$stmt->bindParam(':search_term', $search_term);
$stmt->execute();
$results = $stmt->fetchAll();
foreach ($results as $row) {
    // Process retrieved data
}