SELECT LIKE中有多个关键字

时间:2013-07-24 04:05:52

标签: mysql sql select implode

我的网站上有一个使用GET的搜索功能。我一直在尝试编写一些可以从GET帖子中获取单词然后使用SQL进行搜索的代码。这就是我能够做到的:

$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).")

我不知道我做错了什么。我收到以下错误:

 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

3 个答案:

答案 0 :(得分:0)

在您的情况下,

implode只是字符串的一部分,您需要从引号中取出它:

mysql_query("SELECT * FROM database WHERE ".implode(' AND ', $searchTermBits))

答案 1 :(得分:0)

$lol = mysql_query("SELECT * FROM database WHERE .implode(' AND ', $searchTermBits).")

应该是

$lol = mysql_query("SELECT * FROM database WHERE ". implode(' AND ', $searchTermBits). "")

答案 2 :(得分:0)

你在双引号里面使用php implode函数。就像这样在双引号之外它。

mysql_query("SELECT * FROM database WHERE ".implode(' AND ', $searchTermBits))