PHP + Mysql implements multiple keywords and multiple fields to generate SQL statements. mysqlsql_PHP tutorial

Source: Internet
Author: User
PHP + Mysql implements the function of generating SQL statements with multiple keywords and fields, mysqlsql. PHP + Mysql provides a function for generating SQL statements using multiple keywords and fields. this example describes how to generate SQL statements using multiple keywords and fields in PHP + Mysql. Share the function with PHP + Mysql to generate SQL statements with multiple keywords and fields.

This example describes how to use PHP + Mysql to generate SQL statements using multiple keywords and fields. Share it with you for your reference. The specific implementation method is as follows:

First look at the instance:

The code is as follows:

$ Keyword = "1 2 3 ";
Echo $ SQL = search ($ keyword, "enter_gongyin_pic", "a + B + c"); // function generation, no LIMIT, no ORDER


Generation:

The code is as follows:

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.

The implementation functions are as follows:

The code is as follows:

Function search ($ keyword, $ table, $ field)
{
// ================================================ ============================

// Parameter description:
// Keyword is a keyword, for example, "Beijing Capital direction train ". With or without spaces
// Table is the table name, such as enter_gongyin_pic.
// Field is a combination of fields. for example, you can write the name after you find a field.
// Use name + picdir if you search for more than two
// ================================================ ============================
// First, confirm the field
$ New_field = explode ("+", $ field); // strip by +
$ Field_count = count ($ new_field); // The number of results.


$ Newstring = explode ("", $ keyword); // strip by space
$ Newstring2 = array ();
// Remove the useless space ancestor element from the string
$ 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); // 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) // you can specify 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 the END ****************************


// *********************** If ($ field_count> 1) // find multiple fields: START ****************************
If ($ field_count> 1) // find multiple fields. $ new_field is an array. Has multiple fields
{
If ($ result_count = 1) // judge if it is a key segment
{
$ Newstring_search = $ newstring2 [0]; // $ newstring_search is a 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) // you can specify multiple key fields) ======================================
{
$ SQL = "SELECT *
FROM '$ table'
WHERE ";
$ SQL _add = ""; // add a new field
Foreach ($ new_field as $ key => $ value)
{
If ($ key = 0) // in case of $ new_field [0: 'A 'like' % 100' OR 'A' LIKE '% 100' OR 'A' LIKE' % 100'
{// 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
// (For multi-field queries, such as querying the name + picdir table) start the FOREACH continuous loop, and each execution of ELSE $ new_field [1] $ new_field [2] $ new_field [3].
// 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) 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;
}

I hope this article will help you with PHP programming.


How can I implement unlimited classification in php + mysql, and how can I design a database? An SQL statement reads data and generates an array.

The infinite level you are talking about is not an infinite tree, but an infinite number of classes. there are only two levels, right?

Generally, there are not many categories, and one character can be used for representation, for example, ~ Z encoding, A is news, B is sports ,......

The news category can be set to the character type, with the first digit indicating fatigue, followed by a small class, such as A001 international news, A002 domestic news, B001 basketball ,......

Create two tables. one is a News table with two fields: Code of the category and name of the category. The other is also a news table with two fields: category code and category name.

In your news table, you can use only one character type field.

To generate a tree-like directory output, the start is very simple, as long as your SQL statements are sorted by category, the program checks whether each piece of data is the same as the preceding Category, to determine whether the front is show + or |. that's all.
 

What can I do if a keyword exists in an SQL statement in mysql?

Insert into tb_test ('use') values ('test ');

This is the one on the left of the number key 1 on the top.

Examples in this article describes how to generate SQL statements using multiple keywords and fields in PHP + Mysql. Share with me...

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.