PHP Read Sphinx Example

Source: Internet
Author: User
  1. Check whether Sphinx can connect, cannot retry two times, can connect without MySQL protocol, 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. This is to change the configuration file if it is not connected, write according to the project needs
  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 ';//Collation
  25. $s = Checksphinx ();
  26. $s = new sphinxclient ();
  27. $s->setserver (' sphinx_host ', ' sphinx_port ');
  28. The above two lines of code can also use $s = Checksphinx (), to replace
  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. The corresponding reading according to the printed results
  40. }
  41. ?>
Copy Code

2. Code that uses the MySQL binary network protocol :

  1. Check if Sphinx can connect, cannot retry two times, can connect, use 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. This is to change the configuration file if it is not connected, write according to the project needs
  9. Global $configDefault;
  10. Updateconfig ($configDefault);
  11. $retries + +;
  12. } else {
  13. $flag = false;
  14. Break
  15. }
  16. }
  17. if ($retries >= 2) {
  18. Die ("Administrator.");
  19. }
  20. return $conn;
  21. }
  22. $order _column = ' id desc,time DESC ';//Collation
  23. $conn = mysql_connect ("Sphinx_host:sphinx_port");
  24. The above code can also use $conn = Checksphinx ();
  25. if (! $conn) {
  26. return-1;//Connection not on return status
  27. }
  28. $keyword _sphinx = Iconv ("GBK", "Utf-8", $keyword);
  29. Keyword for index names
  30. $sql = "SELECT * FROM keyword where match (' {$keyword _sphinx} ') Order by {$order _column} limit {$limit _total} option Max_ma tches={$limit _total} ";
  31. $result = @mysql_query ($sql, $conn);
  32. $i = 0;
  33. while (($row = mysql_fetch_array ($result))!== false) {
  34. Var_dump ($row);
  35. The corresponding reading according to the printed results
  36. }
  37. $totals = $this->gettotalfound ($conn);//Get the total number of records
  38. ?>
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?

  • 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.