多个关键字搜索


Multiple keywords search

在我的网站中添加搜索时遇到了麻烦。我想不出怎么办。

我有一张桌子如下:

TABLE A
id     text
1       Hello there whats up. I'm trying to code.
2       there need to be this code

现在我想使用关键词=你好代码

结果应该向我提供这两行,因为这两行都包含关键字的某些部分,如下所示:

id      text
1       **Hello** there whats up. I'm trying to **code**
2       there need to be this **code**

此外,结果应该首先提供匹配关键字的最大数量的行。

我试过这样做,但它只为我提供了一些我想要的结果。

<?php
   $keyword = 'hello code';
   $exloded = explode(' ', $keyword);
   foreach($exploded as value):
      $sth = $db->query("SELECT * FROM A WHERE `text` LIKE :value");
      $sth->execute(array(':value' => '%'.$value.'%'));
      $rows = $sth->fetchAll();
   endforeach;
   echo $rows;
?>

更新

我只是这么做了,对我来说效果很好。但我想知道这是否是完成工作的正确方法。

$keyword = hello code;
$query ="SELECT *, MATCH(`page_content`) AGAINST('$keyword' IN BOOLEAN MODE) AS score      FROM super_pages WHERE MATCH(`page_content`) AGAINST('$keyword' IN BOOLEAN     MODE) ORDER BY score DESC";
$sth = $this->db->query($query);
$result = $sth->fetchAll();

$rows将在表中拥有关键字code匹配的数据。您可以重写代码,将两个关键字匹配为

$keyword = 'hello code';
$exloded = explode(' ', $keyword);
$query = 'SELECT * FROM A ';
$i = 0;
$params = array();
foreach ($exploded as $value):
    if ($i == 0) {
        $query .= ' WHERE `text` LIKE :value_'.$i;
    } else {
        $query .= ' OR `text` LIKE :value_'.$i;
    }
    $params[':value_'.$i] = '%'.$value .'%';
    $i++;
endforeach;
$sth = $db->query($query);
$sth->execute($params);
$rows = $sth->fetchAll();
echo '<pre>';print_r($rows);echo '</pre>';

在循环中构建您的查询(在您提供的关键字上),并在查询中分配唯一的占位符以匹配所有值

编辑以进行全文搜索

使用全文搜索,您可以将完全相同的短语与提供的关键字进行匹配。为了使用全文搜索您需要类型为FULLTEXT的索引。

ALTER TABLE `A` ADD FULLTEXT INDEX `fulltextindex` (`text`); 

查询将类似

$keyword = 'hello code';
$exloded = explode(' ', $keyword);
$where = '';
$i = 0;
$select = array();
$params = array();
foreach ($exploded as $value):
    $select[]= ' MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE) ';
    if ($i == 0) {
        $where  .= ' WHERE MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE)';
    } else {
        $where  .= ' OR MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE)';
    }
    $params[':value_'.$i] =  $value ;
    $i++;
endforeach;
$query ='SELECT *,'. implode( ' + ',$select).' AS score FROM A '.$where.' ORDER BY score DESC';
$sth = $db->query($query);
$sth->execute($params);
$rows = $sth->fetchAll();
echo '<pre>';print_r($rows);echo '</pre>';

上面的代码将产生类似的查询

SELECT *,
MATCH(`text`) AGAINST('hello' IN BOOLEAN MODE)
+ 
MATCH(`text`) AGAINST('code' IN BOOLEAN MODE) AS score
FROM A 
WHERE MATCH(`text`) AGAINST('hello' IN BOOLEAN MODE)
OR  MATCH(`text`) AGAINST('code' IN BOOLEAN MODE)
ORDER BY score DESC

上述查询中的别名score将为每一行及其匹配的分数提供值,因此您可以按降序排列结果,以首先显示得分最高的记录。

注意:您可以在Myisam中使用全文搜索,但对于innodb,您有将您的Mysql升级到5.6,该版本支持在innodb太

您可以使用以下代码:

<?php
   $keyword = 'hello code';
   $exloded = explode(' ', $keyword);
   $sql = "SELECT * FROM A WHERE ";

   foreach($exploded as $value):
      $sql .= "text LIKE '" . $value "%' OR ";
   endforeach;
   // remove last 'OR '
   $sql = substr($sql, -3);
   $result = mysqli_query($con, $sql);

   //................
 ?>