【index.php】php
【find.php】html
<?php header("Content-type:text/html;charset=utf-8"); $keyword = $_GET['word']; //实例化Sphinx对象 $sphinx=new SphinxClient(); //链接sphinx服务器 $sphinx->SetServer("localhost",9312); //拆词 //SPH_MATCH_ALL 和 SPH_MATCH_ANY 的区别: //搜索“LAMP兄弟连”,ALL的结果:完整包含“LAMP兄弟连”才能被搜出来, //单纯包含“LAMP”或单纯包含“兄弟连”的搜索不出来,没有拆词的功能。 //ANY则能够搜索出来拆开后的词的结果。此处使用ANY $sphinx->SetMatchMode(SPH_MATCH_ANY); //经过query方法搜索,“*”表示在全部的索引中搜索,至关于命令行里面的“./indexer --all” $result=$sphinx->query("$keyword","*"); //打印搜索的结果 //echo "<pre>"; //print_r($result); //echo "</pre>"; //上面打印的结果中,数组的 [matches]循环便利,下标就是搜索到的文档的主键Id //使用PHP中的 array_keys()函数便可拿到下标,即:要查找的文档的主键 //print_r(array_keys($result['matches'])); //结果以下:Array([0]=>1) //使用implode或者 join用逗号把查询出来的主键链接起来: $ids = join(',',array_keys($result['matches'])); //echo $ids; //6,7 /*链接数据库的操做*/ $p1 = mysql_connect("localhost","root","123456"); mysql_select_db("test"); mysql_query("set names utf8"); $sql="select * from post where id in ($ids)"; $rst=mysql_query($sql); $opts=array( "before_match"=>"<font color='red'>", "after_match"=>"</font>", ); while($row=mysql_fetch_assoc($rst)){ //print_r($row); //下面是高亮显示所需,具体能够查手册 $final=$sphinx->buildExcerpts($row,"main",$keyword,$opts); echo "<pre>"; print_r($final); echo "</pre>"; } ?>
【当前数据内容】mysql