-
- $keyword="1 2 3";
- echo $sql=search($keyword,"enter_gongyin_pic","a+b+c"); //Function generation, no LIMIT, no ORDER BY
-
Copy code
Generate sql statement:
-
- 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:
-
-
/** - * Query condition construction statement
- * edit: bbs.it-home.org
- */
- function search($keyword,$table,$field)
- {
- /**
- //Formal parameter description:
- //keyword is the keyword, such as "Beijing Capital Direction Train".With spaces or without
- //table is the table name, such as enter_gongyin_pic.
- //Field is a combination of fields. If you are looking for one field, write the name.
- //If you are looking for more than two fields, use name+picdir.
- */
- //First determine the field.
- $new_field=explode("+",$field ); //Press + to strip
- $field_count=count($new_field); //The number of results obtained
-
-
- $newstring=explode(" ",$keyword); //Press space to strip
- $newstring2=array() ;
- //Remove useless spaces and uncle elements from the string
- $i=0;
- foreach ($newstring as $key => $value) {
- if($value!="")
- {
- $newstring2 [$i]=$value;
- $i++;
- }
- }
- //Remove useless spaces and uncle elements from the string,
-
- $result_count=count($newstring2); //Number of results obtained
-
- / /Generate SQL statement
- //* if($field_count==1) //Find 1 field START **
- if($field_count==1) //Find 1 field
- {
- if($result_count==1 ) //Determine if it is a key segment
- {
- $newstring_search=$newstring2[0];
- $sql="SELECT *
- FROM `$table`
- WHERE `".$new_field[0]."` LIKE '% $newstring_search%'";
- }
-
- if($result_count>1) //Judge if there are multiple key segments
- {
-
- $sql="SELECT *
- FROM `$table`
- WHERE ";
- $sql_add= "";
- foreach ($newstring2 as $key => $value)
- {
- if($key==0)
- {
- $sql_add=$sql_add."`".$new_field[0]."` LIKE '%".$value."%'";
- }
- else
- {
- $sql_add=$sql_add." OR `".$new_field[0]."` LIKE '%".$value."%'" ;
- }
- }
$sql=$sql.$sql_add;
- }
- }
//***** if($field_count= =1) //Find 1 fieldEND ******
- //**** if($field_count>1) //Find multiple fields START *****
- if($field_count>1) //Find multiple fields. At this time, $new_field is an array. Has multiple fields
- {
- if($result_count==1) //Determine if it is a key segment
- {
- $newstring_search=$newstring2[0]; //$newstring_search is a keyword
- $sql="SELECT *
- FROM `$table`
- WHERE ";
- $sql_add="";//Newly added field
- foreach ($new_field as $key => $value)
- {
- if($key==0)
- {
- $ sql_add=$sql_add."`".$value."` LIKE '%".$newstring_search."%'";
- }
- else
- {
- $sql_add=$sql_add." OR `".$value."` LIKE '%".$newstring_search."%'";
- }
- }
- $sql=$sql.$sql_add;
- }
- if($result_count>1) //Judge if there are multiple key segments (multiple key Word) ===
- {
- $sql="SELECT *
- FROM `$table`
- WHERE ";
- $sql_add="";//Newly added fields
- foreach ($new_field as $key => $value)
- {
- if($key==0) //When encountering $new_field[0] Example: `a` LIKE '%1%' OR `a` LIKE '%2%' OR `a` LIKE '%3 %'
- { //Nested foreach
- foreach ($newstring2 as $key2 => $value2)
- {
- if($key2==0)
- {
- $sql_add=$sql_add."`".$value. "` LIKE '%".$value2."%'";
- }
- else
- {
- $sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'" ;
- }
- }
- //Nested foreach
- }
- else
- //(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].
- //The corresponding value is $value
- {
- //Nested foreach (multiple fields and multiple keywords)
- foreach ($newstring2 as $key2 => $value2)
- {
- if($key2==0)
- {
- $sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'";
- }
- else
- {
- $sql_add=$sql_add." OR `". $value."` LIKE '%".$value2."%'";
- }
- }
- //Nested foreach
- }
-
- }//end of foreach ($new_field as $key => $value)
- $sql=$sql.$sql_add;
- }//if($result_count>1) end
- }//if($field_count>1) end
- //*** if($field_count>1) //Find more fieldsEND ***
- return $sql;
- }
-
Copy code
|