【发布时间】:2014-12-06 23:43:43
【问题描述】:
在我的网站中添加搜索时遇到问题。我想不出办法来完成它。
我有一张如下表:
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();
【问题讨论】:
-
您考虑过 FULLTEXT 列吗?
-
我尝试了全文搜索但遇到了麻烦所以尝试了这个@Jack