简体   繁体   English

使用MySQL和PHP搜索字符串

[英]Search for a string using MySQL and PHP

I have a table say 3 fields in MySQL. 我有一张桌子说MySQL中的3个字段。 I need to search for a string using PHP My code is like 我需要使用PHP搜索字符串我的代码就像

<?PHP

    //SET THE SEARCH TERM
    $term = "Search Term";

    //QUERY THE TITLE AND CONTENT COLUMN OF THE PAGES TABLE RANK IT BY THE SCORE
    $sql = "SELECT *, MATCH(title, content) AGAINST('". $term ."') as score FROM pages WHERE MATCH (title, content) AGAINST('". $term ."') ORDER BY score DESC";
    $query = mysql_query($sql);

    //BUILD A LIST OF THE RESULTS
    while($result = mysql_fetch_assoc($query)) {
        echo("{$result['title']} - {$result['score']}");
    }

?>

Here it searched for the single word which is exactly in the database. 在这里,它搜索数据库中的单个单词。 I need to search for multiple words..How can I change the above code. 我需要搜索多个单词。如何更改上面的代码。 Can someone suggest some idea. 有人可以提出一些想法吗?

MATCH (title,content) AGAINST ('Search Term' IN BOOLEAN MODE)

would search for one of the words 会搜索其中一个词

MATCH (title,content) AGAINST ('+Search +Term' IN BOOLEAN MODE)

would search both words 会搜索两个词

MATCH (title,content) AGAINST ('-Search +Term' IN BOOLEAN MODE)

would search Term without Search 会搜索不搜索的字词

just split the words and build it with using + - | 只需拆分单词并使用+-|构建它 , whatever needed. ,无论需要什么。

check documentation on : http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html 查看以下文档: http : //dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

You could just add the boolean mode switch to the match - but this does not sort the results by relevance. 您可以只将布尔模式开关添加到匹配项-但这不会按相关性对结果进行排序。

You could try something like..... 您可以尝试类似.....

$words=explode(' ', $term);
$score='CASE ';
$filter=array();
foreach ($words as $try_word) {
   $score.="WHEN MATCH(title, content) AGAINST('$try_word') THEN 1 ";
   $filter[]="MATCH(title, content) AGAINST('$try_word')";
}
$score.="ELSE 0 END CASE";
$qry="SELECT SUM(SCORE), " . ** add explicit list of fields here **
     . " FROM pages WHERE (" . implode(' OR ', $filter) . ")"
     . " GROUP BY " .  ** add explicit list of fields here **
     . " ORDER BY 1 DESC";

Note that this gets highlights the problem that full text searching in mysql is not the ideal solution to the problem - a better approach is to implement a separate table of extracted keywords and a temporary set of search words and perform a query against all three tables: 请注意,这突出了一个问题,即在mysql中进行全文搜索不是解决该问题的理想方法-更好的方法是实现一个单独的提取关键字表和一组临时搜索词,并对所有三个表执行查询:

SELECT COUNT(*) as matched_fields, " . ** add explicit list of fields from page table here ** . "
FROM pages p, keywords k, search_terms s
WHERE k.word=s.word
AND s.session_id='" . some_session_or_request_identifier() . "'
AND k.page_id=p.id
GROUP BY " . ** add explicit list of fields from page table here ** . "
ORDER BY 1 DESC

In addition to simplifying the code somewhat and allowing very large sets of search terms but efficient searching, you can backpopulate relevance scores into the keywords table and use those to generate the score. 除了可以稍微简化代码并允许大量搜索词组但是高效地进行搜索之外,您还可以将相关性得分回填到关键字表中,然后使用这些得分来生成得分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM