- Check whether Sphinx can connect, cannot retry two times, can connect without MySQL protocol, 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 ()) {
- This is to change the configuration file if it is not connected, write according to the project needs
- 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 ';//Collation
- $s = Checksphinx ();
- $s = new sphinxclient ();
- $s->setserver (' sphinx_host ', ' sphinx_port ');
- The above two lines of code can also use $s = Checksphinx (), to replace
- $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 ']);
- The corresponding reading according to the printed results
- }
- ?>
Copy Code
2. Code that uses the MySQL binary network protocol :
- Check if Sphinx can connect, cannot retry two times, 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) {
- This is to change the configuration file if it is not connected, write according to the project needs
- Global $configDefault;
- Updateconfig ($configDefault);
- $retries + +;
- } else {
- $flag = false;
- Break
- }
- }
- if ($retries >= 2) {
- Die ("Administrator.");
- }
- return $conn;
- }
- $order _column = ' id desc,time DESC ';//Collation
- $conn = mysql_connect ("Sphinx_host:sphinx_port");
- The above code can also use $conn = Checksphinx ();
- if (! $conn) {
- return-1;//Connection not on return status
- }
- $keyword _sphinx = Iconv ("GBK", "Utf-8", $keyword);
- Keyword for index names
- $sql = "SELECT * FROM keyword where match (' {$keyword _sphinx} ') Order by {$order _column} limit {$limit _total} option Max_ma tches={$limit _total} ";
- $result = @mysql_query ($sql, $conn);
- $i = 0;
- while (($row = mysql_fetch_array ($result))!== false) {
- Var_dump ($row);
- The corresponding reading according to the printed results
- }
- $totals = $this->gettotalfound ($conn);//Get the total number of records
- ?>
Copy Code
How to get the total number of records reference: SPHINXQL How do I get the number of results? A detailed description of the show meta?
|