-
- //Check whether sphinx can connect, you cannot retry twice, if it can connect, mysql protocol is not used, for reference only
- 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 ()) {
- //Here is to change the configuration file if the connection cannot be made. Write it according to the needs of the project
- //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';//Ordering rules
- //$s = checkSphinx ();
- $s = new SphinxClient ();
- $s->setServer ( 'sphinx_host' , 'sphinx_port');
- //The above two lines of code can also be replaced by $s = checkSphinx ();
- $indexname = "page_keyword";//Index name
- $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']);
- //Read accordingly according to the printed results
- }
- ?>
Copy code
2. Use mysql binary network The code of the protocol:
-
- //Check whether sphinx can connect. You cannot retry twice. If it can connect, use mysql14 protocol
- protected function checkSphinx() {
- $flag = true;
- $retries = 0 ;
- while ( $flag && $retries < 2 ) {
- $conn = mysql_connect ( "{$_ENV ['db_host']}:{$_ENV ['current_sphinx_port']}" );
- if (! $conn) {
- //Here is to change the configuration file if the connection cannot be made. Write it according to the needs of the project
- //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';//Collation rules
- $conn = mysql_connect ("sphinx_host:sphinx_port");
- //The above code can also be replaced by $conn = checkSphinx ();
- if (! $conn) {
- return - 1;//The connection cannot be returned and the status is returned
- }
- $keyword_sphinx = iconv ("gbk", "utf-8", $keyword);
- //keyword is the index name
- $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);
- //Read accordingly based on the printed results
- }
- $totals = $this->getTotalFound ( $conn);//Get the total number of records
- ?>
Copy code
How to get the total number of records Reference: How to get the number of results in sphinxql? Detailed description of show meta?
|