PHP操作Sphinx例項。

2022-07-20 20:27:11 字數 1223 閱讀 6966

<?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 的區別:

//any則可以搜尋出來拆開後的詞的結果。此處使用any

$sphinx->setmatchmode(sph_match_any);

//通過query方法搜尋,「*」表示在所有的索引中搜尋,相當於命令列裡面的「./indexer --all」

$result=$sphinx->query("$keyword","*");

//列印搜尋的結果

//echo "";

//print_r($result);

//echo "

";

//上面列印的結果中,陣列的 [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"=>"",

"after_match"=>"",

);while($row=mysql_fetch_assoc($rst))

?>

PHP讀取sphinx例項

閱讀原文 1.未採用mysql二進位制網路協議的 檢查sphinx是否能連線,不能重試兩次,能則連線,不用mysql協議,僅供參考 function checksphinxnomysql else return s if retries 2 order column id desc,time des...

安裝php擴充套件sphinx

1 安裝 1 先安裝sphinxclient cd usr local src wget tar xzvf sphinx 0.9.9.tar.gz cd sphinx 0.9.9 api libsphinxclient vim sphinxclient.c 找到void sock close int...

PHP操作MongoDB例項

從mysql中把資料匯入到mongodb中做測試.連線資料庫 link mysql connect 127.0.0.1 root 123456 mysql select db jiang link mysql query set names utf 8 sql select id name addr...