What are some potential pitfalls to be aware of when implementing a search function in PHP?

One potential pitfall when implementing a search function in PHP is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection attacks. To mitigate this risk, it is important to use prepared statements or parameterized queries when interacting with a database to prevent malicious input from being executed as SQL code.

// Example of using prepared statements to prevent SQL injection

$searchTerm = $_GET['search'];

// Prepare a SQL statement
$stmt = $pdo->prepare("SELECT * FROM products WHERE name LIKE :searchTerm");

// Bind the search term parameter
$stmt->bindParam(':searchTerm', $searchTerm, PDO::PARAM_STR);

// Execute the statement
$stmt->execute();

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