例如,如果您在两个字段中搜索“未答复的问题”,我希望能够获得结果,包括在其显示的任何字符串中搜索到的单词,并根据相关性列出,像这样(搜索结果示例输出): - 未解答的问题 - 回答问题 - 回答问题 - 未解答的问题 - 未解答的问题 - 问题 - 回答
$k = trim ($_GET['search']);
$i = "";
$terms = explode (" ", $k);
$query = "SELECT * FROM table1 WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
else
$query .= "OR fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
}
// connect
include_once "connect.php"; //connect 2 db
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc ($query)){
//
//
// echo out something here
//
//
}
}else
{
echo "No results found for <b>$k</b>";
}
答案 0 :(得分:0)
要进行全文搜索,您必须:
在表格中创建全文索引(注意字段不能是BLOB)
ALTER TABLE tablename ADD FULLTEXT(field1, field2,...);
在你的情况下:
ALTER TABLE table1 ADD FULLTEXT(fld_title, fld_keyword);
在php更改
$k = trim ($_GET['search']);
$i = "";
$terms = explode (" ", $k);
$query = "SELECT * FROM table1 WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
else
$query .= "OR fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
}
的
$k = trim ($_GET['search']);
$query="SELECT * FROM table1 WHERE MATCH(fld_title, fld_keyword) AGAINST ('".$k."')";
如果你想看到结果的相关性:
$query="SELECT *, MATCH(fld_title, fld_keyword) AGAINST ('".$k."') as relevancy FROM table1 WHERE MATCH(fld_title, fld_keyword) AGAINST ('".$k."')";
MATCH-AGAINST返回一个数字:0表示不匹配或其他取决于匹配。
您可以“按相关性排序”,更改查询以使搜索更具相关性... MATCH(fld_title,fld_keyword)AGAINST('“。$ k。”')&gt; 0.5
只有一个问题:AGAINST部分($ k为你)必须大于3个字符。