$lol = mysql_query("SELECT * FROM database WHERE .implode(' AND ', $searchTermBits).")
should be
$lol = mysql_query("SELECT * FROM database WHERE ". implode(' AND ', $searchTermBits). "")
Question
I have a search function on my site which uses GET. I have been trying to code something that would take words out of the GET post and then search in using SQL. This is what I been able to do:
$id = $_GET["search"];
$searchTerms = explode(' ', $id);
$searchTermBits = array();
foreach ($searchTerms as $term) {
$term = trim($term);
if (!empty($term)) {
$searchTermBits[] = "Name LIKE '%$term%'"
}
}
$lol = mysql_query("SELECT * FROM database WHERE .implode(' AND ', $searchTermBits).")
I don't know what I'm doing wrong. I get the following error:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server
version for the right syntax to use near '(' AND ', Array).' at line 1
Solution
$lol = mysql_query("SELECT * FROM database WHERE .implode(' AND ', $searchTermBits).")
should be
$lol = mysql_query("SELECT * FROM database WHERE ". implode(' AND ', $searchTermBits). "")
OTHER TIPS
implode
is just a part of the string in your case, you need to take it out from quotes:
mysql_query("SELECT * FROM database WHERE ".implode(' AND ', $searchTermBits))
you are using php implode function inside the double quote.place it outside the double quotes like this.
mysql_query("SELECT * FROM database WHERE ".implode(' AND ', $searchTermBits))