PHP+MySQL实现全文搜索查询实例及源码分享

2013 年 10 月 2 日3710

【赛迪网报道】下面是PHP+MySQL实现的全文搜索查询的一段源码实例和操作步骤与大家一起分享。

$arrFTKeyWord =& DV_ChineseWordSegment($keyword);



//$ftKeyWords = implode(’ ‘,$arrFTKeyWord);



$ftKeyWords = ”.implode(’ ‘,$arrFTKeyWord);



//fcicq:中间省略了很多….



$stmt = “SELECT {$SQL_CACHE}



/*SQL_CALC_FOUND_ROWS*/



ft.topicid FROM {$dv}topic_ft AS ft WHERE



MATCH(ft.topicindex) AGAINST(’{$ftKeyWords}’



IN BOOLEAN MODE) “.($boardid>0?”



AND ft.boardid=’{$boardid}’”:”).”



AND ft.posttable=’{$stable}’”;



//in boolean mode,能够保证每一条都查到.



$TopicIDList = ‘0′;



if( $query = $db->query($stmt,array



(’absolutePage’=>$page,’pageSize’=>



$pagesize,’debug’=>QUERY_DEBUG)) )



{



//$Record_Count = $db->scalar(”SELECT FOUND_ROWS()”);



if( $Record_Count < = 0 ){ //没有就输出错误.



head(1,0,0,$arrNavMenu);



showmsg($lang['str_42'].$lang['str_43']);



exit;



}



while( $tmpResult =& $db->fetch_row($query) )



{



//fcicq:注意注意….这里就是保存刚才的id了.这里可以优化一下



$TopicIDList .= “,{$tmpResult[0]}”;



}



}



$PCount = ceil( (float)$Record_Count / (float)$pagesize );



//fcicq:上面用php进行分页预处理,不用管它.



$__dbResults =& $db->getResultSet(”SELECT



{$SQL_CACHE} t.boardid,t.topicid AS rootid,



t.title AS topic,t.expression,t.postusername AS username,t.postuserid,t.dateandtime,t.isbest,



t.locktopic,t.child,t.hits,t.hidename FROM



{$dv}topic AS t WHERE t.topicid IN



({$TopicIDList}) ORDER BY t.topicid DESC”);



//fcicq:这里就直接按主题的先后顺序.

(ORDER BY t.topicid DESC)进行了输出.

讲到这里大家都应该明白了吧,这就是最简单的php,mysql全文搜索分词方式。

0 0