I am trying to make a simple php search using the following query. This works perfectly however the query only gives results for the first word. Would there be a way to get a combined result for all three words using a single query?
$stmt = $pdo->prepare("SELECT * FROM walldb WHERE (wallname LIKE :searchq1 OR :searchq2 OR :searchq3) LIMIT :stat, :limt");
Advertisement
Answer
You need to repeat the expr like val
pattern:
WHERE wallname LIKE :searchq1 OR wallname LIKE :searchq2 OR wallname LIKE :searchq3
What happends with your original code is that it is interpreted as:
WHERE (wallname LIKE :searchq1) OR (:searchq2) OR (:searchq3)
So basically the last two search terms are evaluated in boolean context, as if they were conditions. If one of the search terms starts with 1
, it is evaluated as true (and all rows in the table will be returned); else if neither starts with 1
, the conditions are false, hence only the first condition comes into play (which is what you are seeing).