使用 MySQL 和 PHP 搜索字符串 [英] Search for a string using MySQL and PHP
本文介绍了使用 MySQL 和 PHP 搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,上面写着 MySQL 中的 3 个字段.我需要使用 PHP 搜索字符串我的代码就像
I have a table say 3 fields in MySQL. I need to search for a string using PHP My code is like
<?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)
会搜索其中一个词
MATCH (title,content) AGAINST ('+Search +Term' IN BOOLEAN MODE)
会搜索两个词
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.
检查文档:http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html
这篇关于使用 MySQL 和 PHP 搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文