What potential pitfalls should be avoided when using PHP to interact with a database for search functionality?

One potential pitfall to avoid when using PHP to interact with a database for search functionality is SQL injection. This can occur when user input is not properly sanitized before being included in SQL queries, allowing malicious users to manipulate the query. To prevent SQL injection, always use prepared statements with parameterized queries to securely handle user input.

// Using prepared statements to prevent SQL injection
$searchTerm = $_GET['search'];

// Prepare a statement
$stmt = $pdo->prepare("SELECT * FROM table WHERE column LIKE :searchTerm");
$stmt->execute(['searchTerm' => "%$searchTerm%"]);

// Fetch the results
$results = $stmt->fetchAll();