PHP multi-keyword, multi-field generation of SQL statement functions

Source: Internet
Author: User
    1. $keyword = "1 2 3";
    2. echo $sql =search ($keyword, "Enter_gongyin_pic", "a+b+c"); function generation, no limit, no order by
Copy Code

To generate the SQL statement:

    1. SELECT * from ' enter_gongyin_pic ' WHERE ' a ' like '%1% ' or ' a ' like '%2% ' or ' a ' like '%3% ' or ' B ' like '%1% ' or ' B ' like ' %2% ' or ' B ' like '%3% ' or ' C ' like '%1% ' or ' C ' like '%2% ' or ' C ' like '%3% '
Copy Code

$keyword is obtained by post or get, separated by a space, can be found in multiple fields.

Multiple-keyword, multi-field functions for generating SQL query statements:

  1. /**

  2. * Query Condition construct statement
  3. * edit:bbs.it-home.org
  4. */
  5. function Search ($keyword, $table, $field)
  6. {
  7. /**
  8. Formal parameter Description:
  9. Keyword is the key word, such as "Beijing Capital Direction Train". With spaces or without
  10. Tables are table names, such as Enter_gongyin_pic.
  11. field is a combination of fields, such as finding a field and writing the name.
  12. If you find more than two, use a name+picdir
  13. */
  14. First determine field
  15. $new _field=explode ("+", $field); Press + Peel
  16. $field _count=count ($new _field); Number of results obtained
  17. $newstring =explode ("", $keyword); Split by Space
  18. $newstring 2=array ();
  19. Remove the empty Geshuzu element from the string
  20. $i = 0;
  21. foreach ($newstring as $key = = $value) {
  22. if ($value! = "")
  23. {
  24. $newstring 2[$i]= $value;
  25. $i + +;
  26. }
  27. }
  28. Remove the unused empty Geshuzu element from the string,
  29. $result _count=count ($newstring 2); Number of results obtained
  30. Generating SQL statements
  31. * IF ($field _count==1)//Find 1 fields START * *
  32. if ($field _count==1)//Find 1 fields
  33. {
  34. if ($result _count==1)//Judge if it is a critical segment
  35. {
  36. $newstring _search= $newstring 2[0];
  37. $sql = "SELECT *
  38. From ' $table '
  39. WHERE ' ". $new _field[0]." ' Like '% $newstring _search% ' ";
  40. }
  41. if ($result _count>1)//Judge if multiple key segments
  42. {
  43. $sql = "SELECT *
  44. From ' $table '
  45. WHERE ";
  46. $sql _add= "";
  47. foreach ($newstring 2 as $key = $value)
  48. {
  49. if ($key ==0)
  50. {
  51. $sql _add= $sql _add. " '. $new _field[0]. " ' Like '% '. $value. " %'";
  52. }
  53. Else
  54. {
  55. $sql _add= $sql _add. " OR ' ". $new _field[0]." ' Like '% '. $value. " %'";
  56. }
  57. }

  58. $sql = $sql. $sql _add;

  59. }
  60. }

  61. if ($field _count==1)//Find 1 fields END ******

  62. if ($field _count>1)//Find multiple fields START * * * * *
  63. if ($field _count>1)//Find multiple fields, this time $new_field is an array. have multiple fields
  64. {
  65. if ($result _count==1)//Judge if it is a critical segment
  66. {
  67. $newstring _search= $newstring 2[0]; $newstring _search is the key word
  68. $sql = "SELECT *
  69. From ' $table '
  70. WHERE ";
  71. $sql _add= "";//Add Field
  72. foreach ($new _field as $key = $value)
  73. {
  74. if ($key ==0)
  75. {
  76. $sql _add= $sql _add. " ". $value." ' Like '% '. $newstring _search. " %'";
  77. }
  78. Else
  79. {
  80. $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $newstring _search. " %'";
  81. }
  82. }
  83. $sql = $sql. $sql _add;
  84. }
  85. if ($result _count>1)//Judging if multiple key segments (multiple keywords) = = =
  86. {
  87. $sql = "SELECT *
  88. From ' $table '
  89. WHERE ";
  90. $sql _add= "";//Add Field
  91. foreach ($new _field as $key = $value)
  92. {
  93. if ($key ==0)//Encounters $new_field[0] Example: ' A ' like '%1% ' or ' a ' like '%2% ' or ' a ' like '%3% '
  94. {//nested foreach
  95. foreach ($newstring 2 as $key 2 = $value 2)
  96. {
  97. if ($key 2==0)
  98. {
  99. $sql _add= $sql _add. " ". $value." ' Like '% '. $value 2. " %'";
  100. }
  101. Else
  102. {
  103. $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
  104. }
  105. }
  106. Nested foreach
  107. }
  108. Else
  109. (If it is a multi-field such as Name+picdir table) start the foreach continuous loop, each time you execute else $new _field[1] $new _field[2] $new _field[3].
  110. The corresponding value is $value
  111. {
  112. Nested foreach (multiple fields and multiple keywords)
  113. foreach ($newstring 2 as $key 2 = $value 2)
  114. {
  115. if ($key 2==0)
  116. {
  117. $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
  118. }
  119. Else
  120. {
  121. $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
  122. }
  123. }
  124. Nested foreach
  125. }
  126. }//foreach ($new _field as $key + $value) ends
  127. $sql = $sql. $sql _add;
  128. }//if ($result _count>1) ends
  129. }//if ($field _count>1) ends
  130. if ($field _count>1)//Find multiple fields END * * *
  131. return $sql;
  132. }

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