1.未采用MysqL二进制网络协议的代码:
/**
* PHP读取sphinx实例
*
* @param
* @arrange 网: 512Pic.com
**/
//检查sphinx是否能连接,不能重试两次,能则连接,不用MysqL协议,仅供参考
function checkSphinxNoMysqL() {
$flag = true;
$retries = 0;
while ( $flag && $retries < 2 ) {
$s = new SphinxClient ();
$s->setServer ( $_ENV ['db_host'],$_ENV ['current_sphinx_port'] );
if (! $s->open ()) {
//此处为如果连接不上就更改配置文件,根据项目需要来写
//global $configDefault;
//updateConfig ( $configDefault );
$retries ++;
} else {
$flag = false;
break;
}
}
return $s;
if ($retries >= 2) {
//sendemail or not
return false;
}
}
$order_column = 'id DESC,time DESC';//排序规则
//$s = checkSphinx ();
$s = new SphinxClient ();
$s->setServer ( 'sphinx_host','sphinx_port');
//以上两句代码亦可使用$s = checkSphinx ();来代替
$indexname = "page_keyword";//索引名字
$s->setMatchMode ( SPH_MATCH_PHRASE );
$s->SetSortMode ( SPH_SORT_EXTENDED,$order_column );
$s->setMaxQueryTime ( 100000 );
$s->setLimits ( 0,$limit_total,$limit_total );
$keyword_sphinx = iconv ( "gbk","utf-8",$keyword );
$result = $s->query ( $keyword_sphinx,$indexname );
$s->close ();
if ($result ['total'] > 0) {
var_dump($result ['matches']);
//根据打印出的结果进行相应的读取
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
2.采用MysqL二进制网络协议的代码:
/**
* PHP读取sphinx实例
*
* @param
* @arrange 网: 512Pic.com
**/
//检查sphinx是否能连接,不能重试两次,能则连接,用MysqL14协议
protected function checkSphinx() {
$flag = true;
$retries = 0;
while ( $flag && $retries < 2 ) {
$conn = MysqL_connect ( "{$_ENV ['db_host']}:{$_ENV ['current_sphinx_port']}" );
if (! $conn) {
//此处为如果连接不上就更改配置文件,根据项目需要来写
//global $configDefault;
//updateConfig ( $configDefault );
$retries ++;
} else {
$flag = false;
break;
}
}
if ($retries >= 2) {
die ( "Please contact with administrator." );
}
return $conn;
}
$order_column = 'id DESC,time DESC';//排序规则
$conn = MysqL_connect ( "sphinx_host:sphinx_port" );
//以上代码亦可使用$conn = checkSphinx ();来代替
if (! $conn) {
return - 1;//连接不上返回状态
}
$keyword_sphinx = iconv ( "gbk",$keyword );
//keyword为索引名字
$sql = "select * from keyword where match('{$keyword_sphinx}') order by {$order_column} limit {$limit_total} option max_matches={$limit_total}";
$result = @MysqL_query ( $sql,$conn );
$i = 0;
while ( ($row = MysqL_fetch_array ( $result )) !== false ) {
var_dump($row);
//根据打印出的结果进行相应的读取
}
$totals = $this->getTotalFound ($conn);//获取总记录个数
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/528649.html