- $keyword = "1 2 3";
- echo $sql =search ($keyword, "Enter_gongyin_pic", "a+b+c"); function generation, no limit, no order by
Copy CodeTo generate the 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 a space, can be found in multiple fields. Multiple-keyword, multi-field functions for generating SQL query statements:
/**
- * Query Condition construct statement
- * edit:bbs.it-home.org
- */
- function Search ($keyword, $table, $field)
- {
- /**
- Formal parameter Description:
- Keyword is the key word, such as "Beijing Capital Direction Train". With spaces or without
- Tables are table names, such as Enter_gongyin_pic.
- field is a combination of fields, such as finding a field and writing the name.
- If you find more than two, use a name+picdir
- */
- First determine field
- $new _field=explode ("+", $field); Press + Peel
- $field _count=count ($new _field); Number of results obtained
- $newstring =explode ("", $keyword); Split by Space
- $newstring 2=array ();
- Remove the empty Geshuzu element from the string
- $i = 0;
- foreach ($newstring as $key = = $value) {
- if ($value! = "")
- {
- $newstring 2[$i]= $value;
- $i + +;
- }
- }
- Remove the unused empty Geshuzu element from the string,
- $result _count=count ($newstring 2); Number of results obtained
- Generating SQL statements
- * IF ($field _count==1)//Find 1 fields START * *
- if ($field _count==1)//Find 1 fields
- {
- if ($result _count==1)//Judge if it is a critical segment
- {
- $newstring _search= $newstring 2[0];
- $sql = "SELECT *
- From ' $table '
- WHERE ' ". $new _field[0]." ' Like '% $newstring _search% ' ";
- }
- if ($result _count>1)//Judge if multiple key segments
- {
- $sql = "SELECT *
- From ' $table '
- WHERE ";
- $sql _add= "";
- foreach ($newstring 2 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 fields END ******
- if ($field _count>1)//Find multiple fields START * * * * *
- if ($field _count>1)//Find multiple fields, this time $new_field is an array. have multiple fields
- {
- if ($result _count==1)//Judge if it is a critical segment
- {
- $newstring _search= $newstring 2[0]; $newstring _search is the key word
- $sql = "SELECT *
- From ' $table '
- WHERE ";
- $sql _add= "";//Add 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)//Judging if multiple key segments (multiple keywords) = = =
- {
- $sql = "SELECT *
- From ' $table '
- WHERE ";
- $sql _add= "";//Add Field
- foreach ($new _field as $key = $value)
- {
- if ($key ==0)//Encounters $new_field[0] Example: ' A ' like '%1% ' or ' a ' like '%2% ' or ' a ' like '%3% '
- {//nested foreach
- foreach ($newstring 2 as $key 2 = $value 2)
- {
- if ($key 2==0)
- {
- $sql _add= $sql _add. " ". $value." ' Like '% '. $value 2. " %'";
- }
- Else
- {
- $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
- }
- }
- Nested foreach
- }
- Else
- (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].
- The corresponding value is $value
- {
- Nested foreach (multiple fields and multiple keywords)
- foreach ($newstring 2 as $key 2 = $value 2)
- {
- if ($key 2==0)
- {
- $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
- }
- Else
- {
- $sql _add= $sql _add. " OR ' ". $value." ' Like '% '. $value 2. " %'";
- }
- }
- Nested foreach
- }
- }//foreach ($new _field as $key + $value) ends
- $sql = $sql. $sql _add;
- }//if ($result _count>1) ends
- }//if ($field _count>1) ends
- if ($field _count>1)//Find multiple fields END * * *
- return $sql;
- }
Copy Code |