Example of php reading sphinx
- // Check whether sphinx can be connected. you cannot try again twice. If yes, you can connect without the mysql protocol. it is 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 ()){
- // Change the configuration file if the connection fails.
- // 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 '; // sorting rule
- // $ S = checkSphinx ();
- $ S = new SphinxClient ();
- $ S-> setServer ('hsf-_host', 'hsf-_port ');
- // The above two statements 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 data based on the printed result
- }
- ?>
2. code using the mysql binary network protocol:
- // Check whether sphinx can be connected. If yes, try again. use the 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 ){
- // Change the configuration file if the connection fails.
- // 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 '; // sorting rule
- $ Conn = mysql_connect ("sphinx_host: sphinx_port ");
- // The above code can also be replaced by $ conn = checkSphinx ();
- If (! $ Conn ){
- Return-1; // The status is returned if the connection fails.
- }
- $ Keyword_sphinx = iconv ("gbk", "UTF-8", $ keyword );
- // Keyword indicates the index name.
- $ SQL = "select * from keyword where match ('{$ keyword_sph0000}') 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 data based on the printed result
- }
- $ Totals = $ this-> getTotalFound ($ conn); // obtain the total number of records
- ?>
For details about how to obtain the total number of records, refer to: how does sphinxql obtain the number of results? What are the detailed descriptions of show meta? |