I have a search feature on my website, which permits to users to define keywords to be searched. Search processing occurs once a day, so the keywords are stored in a table.
The solution I was using until now is to store the keywords as a SQL query:
+---------+--------------------------------------------+------+
| user_id | subquery | type |
+---------+--------------------------------------------+------+
| 1 | desc LIKE '%test1%' OR desc LIKE '%test1%' | buy |
+---------+--------------------------------------------+------+
(subquery column can content as much as keywords the user wants)
Then perform the research by doing:
$req = "SELECT user_id, subquery, type FROM table";
$res = $pdo->prepare($req);
$res->execute();
while ($rows = $res->fetch(PDO::FETCH_ASSOC)) {
$req2 = "SELECT * FROM items_tbl WHERE (".$rows['subquery'].") and type = '".$rows['type']."'";
$res2 = $pdo->prepare($req2);
$res2->execute();
while($rows2 = $res2->fetch(PDO::FETCH_ASSOC)) {
Do stuff...
}
}
I would like now to use a prepared statement to catch the exceptions, so I have tried:
while ($rows = $res->fetch(PDO::FETCH_ASSOC)) {
$req2 = "SELECT * FROM items_tbl WHERE (:subquery) and type=:type";
$res2 = $pdo->prepare($req2);
$res2->execute(array('subquery' => $rows['subquery'], 'type' => $rows['type']));
while($rows2 = $res2->fetch(PDO::FETCH_ASSOC)) {
Do stuff...
}
}
It returns nothing, WHERE (:subquery) is not filled by the appropriate content.
What is the correct approach for doing that?