Home  >  Article  >  Database  >  Mysql full-text search Chinese solution and example code sharing

Mysql full-text search Chinese solution and example code sharing

黄舟
黄舟Original
2017-03-06 13:34:331374browse

This article mainly introduces the relevant information of mysql full-text search Chinese solution and example code. Friends in need can refer to

mysql full-text search Chinese solution

Recently, the company's project requires such functions to retrieve Chinese in the database. It is very tricky. Inquiry on the Internet, similar articles to find, here and record, hope to help everyone,

#Example code:

<?php
/*
mysql全文检索中文解决方案!
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set(&#39;display_errors&#39;, &#39;1&#39;);
//数据库支持
class SaeMysql{
//phpmysql操作类
}
$DBS=new SaeMysql;
//数据加入
echo &#39;2&#39;;
/*创建数据表*/
$DBS->runSql(&#39;CREATE TABLE IF NOT EXISTS `ces_articles` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT \&#39;\&#39;,
`url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `url` (`url`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC&#39;);
/*数据加入*/
$title=&#39;我爱大家啊,大家好&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
$title=&#39;中国是什么&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
$title=&#39;http://ask.1912news.com&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
$title=&#39;问答系统&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
$title=&#39;1912网&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
$title=&#39;零九网络&#39;;
$DBS->runSql(&#39;INSERT INTO `ces_articles` (id,title,url) VALUES (0,\&#39;&#39;. $title.&#39;\&#39;,\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($title))).&#39;\&#39;)&#39;);
//搜索:
$_GET[&#39;q&#39;]="中国";
echo &#39;q&#39;;
if(isset($_GET[&#39;q&#39;])){$sql=&#39; match(url) against (\&#39;&#39;.str_replace(&#39;\u&#39;,&#39;u&#39;,trim(json_encode($_GET[&#39;q&#39;]))).&#39;\&#39; IN BOOLEAN MODE)&#39;;}
$query = $DBS->getData(&#39;SELECT * FROM `ces_articles` where &#39;.$sql.&#39; LIMIT 10&#39;);
echo &#39;q&#39;;
if($query){
foreach ($query as $article){
echo $article[&#39;id&#39;];
}
}

?>

The above is the Chinese solution and example code sharing of mysql full-text search, more related content Please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn