Example
<?php
$search = trim($_GET['q'] ?? '');
if ($search === '') {
$results = [];
} else {
$stmt = $pdo->prepare(
'SELECT id, title, excerpt
FROM articles
WHERE title LIKE :term
ORDER BY title ASC
LIMIT 20'
);
$stmt->execute(['term' => '%' . $search . '%']);
$results = $stmt->fetchAll();
}
Explanation
The search term is passed as a bound value rather than concatenated into SQL. LIMIT 20 prevents an unbounded result set.
Production considerations
Escape output when rendering titles and excerpts. For large datasets, consider full-text indexing and pagination rather than increasingly broad wildcard searches.