mysql 全文檢索中文解決方法及實例代碼
更新時間:2017年02月22日 16:32:09 投稿:lqh
這篇文章主要介紹了mysql 全文檢索中文解決方法及實例代碼的相關(guān)資料,需要的朋友可以參考下
mysql 全文檢索中文解決方法
最近公司項目要求這樣的功能,在數(shù)據(jù)庫中檢索中文,很是棘手,上網(wǎng)查詢下資料,找的類似文章,這里及記錄下,希望能幫助到大家,
實例代碼:
<?php
/*
mysql全文檢索中文解決方案!
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', '1');
//數(shù)據(jù)庫支持
class SaeMysql{
//phpmysql操作類
}
$DBS=new SaeMysql;
//數(shù)據(jù)加入
echo '2';
/*創(chuàng)建數(shù)據(jù)表*/
$DBS->runSql('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 \'\',
`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');
/*數(shù)據(jù)加入*/
$title='我愛大家啊,大家好';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='中國是什么';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='http://ask.1912news.com';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='問答系統(tǒng)';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='1912網(wǎng)';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='零九網(wǎng)絡(luò)';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
//搜索:
$_GET['q']="中國";
echo 'q';
if(isset($_GET['q'])){$sql=' match(url) against (\''.str_replace('\u','u',trim(json_encode($_GET['q']))).'\' IN BOOLEAN MODE)';}
$query = $DBS->getData('SELECT * FROM `ces_articles` where '.$sql.' LIMIT 10');
echo 'q';
if($query){
foreach ($query as $article){
echo $article['id'];
}
}
?>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
MySQL NDB Cluster關(guān)于Nginx stream的負(fù)載均衡配置方式
這篇文章主要介紹了MySQL NDB Cluster關(guān)于Nginx stream的負(fù)載均衡配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
mysql觸發(fā)器實時檢測一條語句進行備份刪除思路詳解
遇到過這樣一個需求,在一張表里會不時出現(xiàn) “違規(guī)” 字樣的字段,需要在出現(xiàn)這個字段的時候,把整行的數(shù)據(jù)刪掉,針對這個需求我們該如何操作呢,下面跟隨小編看下mysql觸發(fā)器實時檢測一條語句進行備份刪除的解決思路,一起看看吧2021-09-09
mysql之跨庫關(guān)聯(lián)查詢(dblink)問題
這篇文章主要介紹了mysql之跨庫關(guān)聯(lián)查詢(dblink)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

