Example of php reading sphinx-php Tutorial

Source: Internet
Author: User
Example of php reading sphinx

  1. // 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.
  2. Function checkSphinxNoMysql (){
  3. $ Flag = true;
  4. $ Retries = 0;
  5. While ($ flag & $ retries <2 ){
  6. $ S = new SphinxClient ();
  7. $ S-> setServer ($ _ ENV ['DB _ host'], $ _ ENV ['current _ sphinx_port ']);
  8. If (! $ S-> open ()){
  9. // Change the configuration file if the connection fails.
  10. // Global $ configDefault;
  11. // UpdateConfig ($ configDefault );
  12. $ Retries ++;
  13. } Else {
  14. $ Flag = false;
  15. Break;
  16. }
  17. }
  18. Return $ s;
  19. If ($ retries> = 2 ){
  20. // Sendemail or not
  21. Return false;
  22. }
  23. }
  24. $ Order_column = 'Id DESC, time DESC '; // sorting rule
  25. // $ S = checkSphinx ();
  26. $ S = new SphinxClient ();
  27. $ S-> setServer ('hsf-_host', 'hsf-_port ');
  28. // The above two statements can also be replaced by $ s = checkSphinx ();
  29. $ Indexname = "page_keyword"; // Index Name
  30. $ S-> setMatchMode (SPH_MATCH_PHRASE );
  31. $ S-> SetSortMode (SPH_SORT_EXTENDED, $ order_column );
  32. $ S-> setMaxQueryTime (100000 );
  33. $ S-> setLimits (0, $ limit_total, $ limit_total );
  34. $ Keyword_sphinx = iconv ("gbk", "UTF-8", $ keyword );
  35. $ Result = $ s-> query ($ keyword_sphinx, $ indexname );
  36. $ S-> close ();
  37. If ($ result ['total']> 0 ){
  38. Var_dump ($ result ['matches']);
  39. // Read data based on the printed result
  40. }
  41. ?>

2. code using the mysql binary network protocol:

  1. // Check whether sphinx can be connected. If yes, try again. use the mysql14 protocol.
  2. Protected function checkSphinx (){
  3. $ Flag = true;
  4. $ Retries = 0;
  5. While ($ flag & $ retries <2 ){
  6. $ Conn = mysql_connect ("{$ _ ENV ['DB _ host'] }:{$ _ ENV ['current _ sphinx_port ']}");
  7. If (! $ Conn ){
  8. // Change the configuration file if the connection fails.
  9. // Global $ configDefault;
  10. // UpdateConfig ($ configDefault );
  11. $ Retries ++;
  12. } Else {
  13. $ Flag = false;
  14. Break;
  15. }
  16. }
  17. If ($ retries> = 2 ){
  18. Die ("Please contact with administrator .");
  19. }
  20. Return $ conn;
  21. }
  22. $ Order_column = 'Id DESC, time DESC '; // sorting rule
  23. $ Conn = mysql_connect ("sphinx_host: sphinx_port ");
  24. // The above code can also be replaced by $ conn = checkSphinx ();
  25. If (! $ Conn ){
  26. Return-1; // The status is returned if the connection fails.
  27. }
  28. $ Keyword_sphinx = iconv ("gbk", "UTF-8", $ keyword );
  29. // Keyword indicates the index name.
  30. $ SQL = "select * from keyword where match ('{$ keyword_sph0000}') order by {$ order_column} limit {$ limit_total} option max_matches = {$ limit_total }";
  31. $ Result = @ mysql_query ($ SQL, $ conn );
  32. $ I = 0;
  33. While ($ row = mysql_fetch_array ($ result ))! = False ){
  34. Var_dump ($ row );
  35. // Read data based on the printed result
  36. }
  37. $ Totals = $ this-> getTotalFound ($ conn); // obtain the total number of records
  38. ?>

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.