[英]php multiple keyword search
Need help with my php search for multiple keywords, the code below displays no results, but when I remove the explode function and foreeach construct, it works, but only displays results of 1 keyword. 在我的php搜索多个关键字时需要帮助,下面的代码不显示任何结果,但是当我删除explode函数并指定结构时,它可以工作,但仅显示1个关键字的结果。
For example, I have a list of keywords in a column: apple, red, blue, book, yellow bird. 例如,我在列中有一个关键字列表:苹果,红,蓝,书,黄鸟。
Using the code below, if I typed red apple in the search box it would yield no results, I'd like for it to display all results containing the keyword I typed. 使用下面的代码,如果我在搜索框中键入红苹果将不会产生任何结果,我希望它显示包含我键入的关键字的所有结果。 Thanks
谢谢
$search = $_POST['search'];
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim ($search);
$search = explode(",",$search);
$query=$db->prepare("SELECT * FROM thread WHERE title like '%".$search."%' OR keyword like '%".$search."%' OR description like '%".$search."%'");
foreach($search as $k)
{$query=$db->prepare("OR keyword LIKE '%$k%' OR title LIKE '%$k%' OR description LIKE '%$k%'");}
$query->execute(array('title' => $search, 'description'=>$search, 'keyword'=>$search));
$query->setFetchMode(PDO::FETCH_ASSOC);
$List="";
if($query->rowCount()<1)
{$List.="<div class='containerr'>No results found.</div>";}
else
{
while($row=$query->fetch())
{
$vid=$row['id'];
$user=$row['username'];
$preview=$row['preview'];
$names=$row['names'];
$good=$row['good'];
$date=$row['date'];
$date=date('M-d-Y', strtotime($date));
$title=$row['title'];
$List.='<div class="LISTT"><a href="mp.php?id='.$vid.'">'.$preview.'<br/>
<label>'.$title.'</label><br/>
<label style="color:black;">Added:</label> <label style="color:#666666">'.$date.'</label></a></div>';
}
The problem is, that you overwrite your SQL instead of appending to it. 问题是,您覆盖了SQL而不是将其附加到SQL。 In addition to that, you run the title search for the redundant combined value, which I guess is not what you want.
除此之外,您还可以对多余的组合值进行标题搜索,我想这不是您想要的。 Try this:
尝试这个:
$search = $_POST['search'];
$search = strtoupper($search);
$search = strip_tags($search);
$search = explode(",",$search);
$sql=array("SELECT * FROM thread WHERE ");
foreach($search as $k) {
$k=trim($k);
$sql[]="keyword LIKE '%$k%' OR title LIKE '%$k%' OR description LIKE '%$k%'";
}
$sql=implode(' OR ', $sql);
$query=$db->prepare($sql);
$query->execute(array());
$query->setFetchMode(PDO::FETCH_ASSOC);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.