如果在单词之间使用间隙,则搜索查询显示零结果。例如:
DB:
TABLE NAME - peoples
-----------------------------------
ID - forename - surname
-----------------------------------
1 Johny Depp
2 Kobe Bryant
3 Random Name
4 Mark Zuckerberg
etc
查询:
$sqlnames="SELECT id, forename, surname FROM peoples WHERE forename LIKE '%$search_keyword%' OR surname LIKE '%$search_keyword%' LIMIT 20";
例如,如果在我输入的输入中: Johny 它按预期显示: John Depp ,或者如果我输入姓氏: Depp ,它也按预期显示,但如果我输入全名: Johny Depp ,则结果为零。我怎么能解决这个问题?
抱歉英语不好,谢谢你的回答!
答案 0 :(得分:1)
尝试以下方法:
$sqlnames="SELECT id, forename, surname,concat(forename,' ',surname) as fullname FROM peoples WHERE fullname LIKE '%$search_keyword%' OR forename LIKE '%$search_keyword%' OR surname LIKE '%$search_keyword%' LIMIT 20";
答案 1 :(得分:1)
这是您的查询。
$sqlnames="SELECT id, forename, surname FROM peoples WHERE forename LIKE '%$search_keyword%' OR surname LIKE '%$search_keyword%' OR
CONCAT(forename, ' ', surname ) LIKE '%$search_keyword%'
LIMIT 20";
答案 2 :(得分:1)
当您搜索全名时,您正在搜索姓氏中的“全名”或姓氏中的“全名”。这就是为什么你没有得到结果。
尝试添加其他条件OR CONCAT(forename, ' ', surname) LIKE '%$search_keyword%'
答案 3 :(得分:1)
您需要拆分单词然后搜索:
$word_array = explode(" ",$search_keyword);
$sqlnames="SELECT id, forename, surname FROM peoples WHERE";
$count = 0;
foreach($word_array as $value){
if($count == 0){
$sqlnames .= " forname LIKE '" . $value ."'";
}
else{
$sqlnames .= " OR forname LIKE '" . $value ."'";
}
$count = 1;
$sqlnames .= " OR surname LIKE '" . $value ."'";
}
$sqlnames .="LIMIT 20";