mysql 全文檢索中文解決方法
最近公司項(xiàng)目要求這樣的功能,在數(shù)據(jù)庫(kù)中檢索中文,很是棘手,上網(wǎng)查詢下資料,找的類似文章,這里及記錄下,希望能幫助到大家,
實(shí)例代碼:
?php /* mysql全文檢索中文解決方案! */ error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set('display_errors', '1'); //數(shù)據(jù)庫(kù)支持 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='中國(guó)是什么'; $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']="中國(guó)"; 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']; } } ?>
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
標(biāo)簽:鄂州 南寧 中衛(wèi) 浙江 阿里 張家界 定西 上海
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql 全文檢索中文解決方法及實(shí)例代碼》,本文關(guān)鍵詞 mysql,全文檢索,中文,解決,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。