What potential pitfalls should be considered when using PHP to query a database for search results?

One potential pitfall when using PHP to query a database for search results is the risk of SQL injection attacks if user input is not properly sanitized. To prevent this, always use prepared statements with parameterized queries to securely pass user input to the database.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a statement with a parameterized query
$stmt = $pdo->prepare('SELECT * FROM mytable WHERE column = :search_term');

// Bind the search term to the parameter
$stmt->bindParam(':search_term', $_GET['search_term']);

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

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