php - 通过短语或单独的关键字搜索 mysql 数据库

标签 php mysql database

好的,当用户在搜索输入中键入搜索短语时,我希望它与输入的确切短语或关键词相匹配。 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/

相关文章:

php - 如何从 www 目录之外运行带有 wamp 的 php 文件?

php - 注册和登录最佳实践

mysql - MySQL 中的多个内部连接?

php - 如果 for 循环从 mysql 数据库重复单行,我该怎么办?

php - PayPal API CreateRecurringPaymentsProfile 拒绝工作

php - 在 PHP 上使用 curl 为 Web API 设置路径 HTTP GET 请求

php - 什么更快 - 数组与数据库

mysql - 使用 CONCAT 显示空值将多列中的值更新为一列

c# - 适用于 Windows 8 的简单数据库

java - 无法检索过程的元数据