Home Backend Development PHP Tutorial PHP multi-keyword and multi-field function to generate SQL statements

PHP multi-keyword and multi-field function to generate SQL statements

Jul 25, 2016 am 08:55 AM

  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

Generate 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 spaces, and can be searched in multiple fields.

Function to generate SQL query statements with multiple keywords and multiple fields:

  1. /**

  2. * Query condition construction statement
  3. * edit: bbs.it-home.org
  4. */
  5. function search($keyword,$table,$field)
  6. {
  7. /**
  8. //Formal parameter description:
  9. //keyword is the keyword, such as "Beijing Capital Direction Train".With spaces or without
  10. //table is the table name, such as enter_gongyin_pic.
  11. //Field is a combination of fields. If you are looking for one field, write the name.
  12. //If you are looking for more than two fields, use name+picdir.
  13. */
  14. //First determine the field.
  15. $new_field=explode("+",$field ); //Press + to strip
  16. $field_count=count($new_field); //The number of results obtained
  17. $newstring=explode(" ",$keyword); //Press space to strip
  18. $newstring2=array() ;
  19. //Remove useless spaces and uncle elements from the string
  20. $i=0;
  21. foreach ($newstring as $key => $value) {
  22. if($value!="")
  23. {
  24. $newstring2 [$i]=$value;
  25. $i++;
  26. }
  27. }
  28. //Remove useless spaces and uncle elements from the string,
  29. $result_count=count($newstring2); //Number of results obtained
  30. / /Generate SQL statement
  31. //* if($field_count==1) //Find 1 field START **
  32. if($field_count==1) //Find 1 field
  33. {
  34. if($result_count==1 ) //Determine if it is a key segment
  35. {
  36. $newstring_search=$newstring2[0];
  37. $sql="SELECT *
  38. FROM `$table`
  39. WHERE `".$new_field[0]."` LIKE '% $newstring_search%'";
  40. }
  41. if($result_count>1) //Judge if there are multiple key segments
  42. {
  43. $sql="SELECT *
  44. FROM `$table`
  45. WHERE ";
  46. $sql_add= "";
  47. foreach ($newstring2 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 fieldEND ******

  62. //**** if($field_count>1) //Find multiple fields START *****
  63. if($field_count>1) //Find multiple fields. At this time, $new_field is an array. Has multiple fields
  64. {
  65. if($result_count==1) //Determine if it is a key segment
  66. {
  67. $newstring_search=$newstring2[0]; //$newstring_search is a keyword
  68. $sql="SELECT *
  69. FROM `$table`
  70. WHERE ";
  71. $sql_add="";//Newly added 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) //Judge if there are multiple key segments (multiple key Word) ===
  86. {
  87. $sql="SELECT *
  88. FROM `$table`
  89. WHERE ";
  90. $sql_add="";//Newly added fields
  91. foreach ($new_field as $key => $value)
  92. {
  93. if($key==0) //When encountering $new_field[0] Example: `a` LIKE '%1%' OR `a` LIKE '%2%' OR `a` LIKE '%3 %'
  94. { //Nested foreach
  95. foreach ($newstring2 as $key2 => $value2)
  96. {
  97. if($key2==0)
  98. {
  99. $sql_add=$sql_add."`".$value. "` LIKE '%".$value2."%'";
  100. }
  101. else
  102. {
  103. $sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'" ;
  104. }
  105. }
  106. //Nested foreach
  107. }
  108. else
  109. //(If it is multi-field, such as checking the name+picdir table) Start a FOREACH continuous loop, and execute ELSE each time $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 ($newstring2 as $key2 => $value2)
  114. {
  115. if($key2==0)
  116. {
  117. $sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'";
  118. }
  119. else
  120. {
  121. $sql_add=$sql_add." OR `". $value."` LIKE '%".$value2."%'";
  122. }
  123. }
  124. //Nested foreach
  125. }
  126. }//end of foreach ($new_field as $key => $value)
  127. $sql=$sql.$sql_add;
  128. }//if($result_count>1) end
  129. }//if($field_count>1) end
  130. //*** if($field_count>1) //Find more fieldsEND ***
  131. return $sql;
  132. }

Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles