Rumah > pangkalan data > tutorial mysql > mysql 全文检索中文解决方法及实例代码分享

mysql 全文检索中文解决方法及实例代码分享

黄舟
Lepaskan: 2017-03-06 13:34:33
asal
1406 orang telah melayarinya

这篇文章主要介绍了mysql 全文检索中文解决方法及实例代码的相关资料,需要的朋友可以参考下

mysql 全文检索中文解决方法

            最近公司项目要求这样的功能,在数据库中检索中文,很是棘手,上网查询下资料,找的类似文章,这里及记录下,希望能帮助到大家,

实例代码:   

<?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;];
}
}

?>
Salin selepas log masuk

以上就是mysql 全文检索中文解决方法及实例代码分享的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan