好的,当用户在搜索输入中键入搜索短语时,我希望它与输入的确切短语或关键词相匹配。 IE 所以帖子的标题是“搜索数据库”
$searchVal = "Search database";
WHERE post_title LIKE '%" . $searchVal . "%'
上述代码未找到匹配项,因为标题在搜索数据库之间有“the”。
我怎样才能让它匹配。 我想也许使用爆炸,但我收到错误:
$sVals = explode(" ", $searchVal);
foreach ($sVals as $s) {
$querySearchVals .= "OR post_title LIKE '%" . $s . "%'";
}
希望这是有道理的。 干杯
最佳答案
也许这会有所帮助
$search_key = $_POST['searchkey']; //take the search text from the post method
$search_words = array();
$search_words = explode(" ",$search_key);
$q_string = "";
$last_index = intval(count($search_Words)) - 1;
for($i = 0; $i<count($search_Words);$i++)
{
$q_string = $q_string . " post_title LIKE '%$search_words[$i]%' ";
if($i != $last_index)
$q_string = $q_string . " OR ";
}
为了使其更加准确,您可以跳过 A、The、An 等文章
关于php - 通过短语或单独的关键字搜索 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897035/