First look at the instance:
$ Keyword = "1 2 3 ";
Echo $ SQL = search ($ keyword, "enter_gongyin_pic", "A + B + C"); // function generation, no limit, no order
Generation:
Select * From 'enter _ gongyin_pic 'where 'a' like' % 100' or 'A' like '% 100' or 'A' like' % 100' or 'B' like '% 100' or' B 'like' % 100' or 'B' like '% 100' or 'C' like' % 100' or 'C' like '% 100' or 'C' like '% 3%'
$ Keyword is obtained by post or get. Multiple fields can be searched separately by space.
Attached Function
Function search ($ keyword, $ table, $ field) {// ============================================== ==================================//author: shi Wei QQ: 43592111 // parameter description: // keyword is a keyword, such as "Beijing Capital direction train ". Table names with spaces or without a // table, such as enter_gongyin_pic. // Field is a combination of fields, if you search for a field, write the name //. If you search for more than two fields, use name + picdir // ==================. ==================================================================/// first confirm field $ new_field = explode ("+ ", $ field); // peel by + $ field_count = count ($ new_field); // obtain the number of results $ newstring = explode ("", $ keyword ); // strip by space $ newstring2 = array (); // remove the useless space ancestor element $ I = 0; foreach ($ newstring as $ key => $ value) {if ($ value! = "") {$ Newstring2 [$ I] = $ value; $ I ++ ;}// remove the useless space ancestor element from the string, $ result_count = count ($ newstring2 ); // obtain the number of results. // The following SQL statement is generated. // ********************** if ($ field_count = 1) // find the start field ***************************** if ($ field_count = = 1) // find 1 field {if ($ result_count = 1) // judge 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) // determine if multiple key segments are selected {$ 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 the end ****************************//*** * ******************* if ($ Field _ Count> 1) // find multiple fields, such as start *************************** if ($ field_count> 1) // find multiple fields. $ new_field is an array at this time. Has multiple fields {if ($ result_count = 1) // determines if it is a key segment {$ newstring_search = $ newstring2 [0]; // $ newstring_search is the keyword $ SQL = "select * from '$ table' where"; $ SQL _add = ""; // Add a new 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 )/ /Determine if multiple key segments (multiple keywords) ========================={ $ SQL = "select * from '$ table' where"; $ SQL _add = ""; // Add a new field foreach ($ new_field as $ key => $ value) {if ($ key = 0) // example of $ new_field [0: 'A 'like' % 100' or 'A' like '% 100' or 'A' like' % 100' {// nested foreach ($ newstring2 as $ key2 => $ value2) {if ($ key2 = 0) {$ SQL _add = $ SQL _add. "'". $ value. "'like' % ". $ value2. "% '";} else {$ SQL _add = $ SQL _add. "Or '". $ value. "l Ike '% ". $ value2. "% '" ;}/// nested foreach} else // (for example, querying the name + picdir table with multiple fields) starts a continuous foreach loop, run else $ new_field [1] $ new_field [2] $ new_field [3] each time. // The corresponding value is $ value {// nested foreach (Multi-field and multi-Keyword) 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 }// foreach ($ new_field as $ key =>$ value) end $ SQL = $ SQL. $ SQL _add;} // if ($ result_count> 1) end} // if ($ field_count> 1) end // *********************** if ($ field_count> 1) // find multiple fields: End *************************** return $ SQL ;}