mysql 全文檢索中文解決方法及實例代碼。本站提示廣大學習愛好者:(mysql 全文檢索中文解決方法及實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql 全文檢索中文解決方法及實例代碼正文
mysql 全文檢索中文解決方法
最近公司項目要求這樣的功能,在數據庫中檢索中文,很是棘手,上網查詢下資料,找的類似文章,這裡及記錄下,希望能幫助到大家,
實例代碼:
<?php /* mysql全文檢索中文解決方案! */ error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set('display_errors', '1'); //數據庫支持 class SaeMysql{ //phpmysql操作類 } $DBS=new SaeMysql; //數據加入 echo '2'; /*創建數據表*/ $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'); /*數據加入*/ $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='問答系統'; $DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')'); $title='1912網'; $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))).'\')'); //搜索: $_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']; } } ?>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
[db:作者簡介][db:原文翻譯及解析]