Php query and multi-condition query, php Condition

Source: Internet
Author: User

Php query and multi-condition query, php Condition

Single-condition query:

1. First, you must have a table to display the data in the table:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

:

A table that hasn't been changed

2. Use the from form to input the table and click query:

<Form action = "shouye. php "method =" post "> <div> input name: <input type = "text" name = "name"/> <input type = "submit" value = "query"/> </div> </form>

3. Create a keyword query:

<? Php // implement two logics // 1. if no post data exists. query all // 2. if there is post data. query $ db = new mysqli ("localhost", "root", "12345678", "heiheihei") according to the condition; // connect to the database $ tj = "1 = 1 "; $ name = ""; // always set. If no data is written, set the condition to 1 = 1. This condition is used to find all the data. // if you write data, query if (! Empty ($ _ POST) {$ name = $ _ POST ['name']; $ tj = "name like '% {$ name} % '";} // concatenates conditions into SQL statements $ SQL = "select * from contacts WHERE {$ tj}"; echo $ SQL; // check out $ r = $ db-> query ($ SQL); // pass the value if ($ r) // start to judge {// $ attr has received the value, now you only need to get his index. while ($ attr = $ r-> fetch_row () {// special keyword query $ str = str_replace ($ name, "<mark> {$ name} </mark>", $ attr [1]); // search for replacement, such as ctrl + f // substr_replace (); replace // substr () at the specified position; intercept the string $ gname = "sel Ect gname from groups WHERE gid = '{$ attr [3]}' "; // gid in the grouping table, and I clicked $ nresult = $ db-> query ($ gname); $ gname = $ nresult-> fetch_row (); $ nation = $ gname [0]; echo "<tr> <td >{$ attr [0] }</td> <td >{$ str }</td> <td >{$ attr [2]} </td> <td >{$ nation} </td>?>

Figure:

Multi-condition query:

Same as before;

Php statements:

<? Php // implement two logics // 1. if no post data exists. query all // 2. if there is post data. query $ db = new mysqli ("localhost", "root", "12345678", "heiheihei") according to the condition; // connect to the database $ tj1 = "1 = 1 "; $ tj2 = "1 = 1"; // constant of the two conditions $ name = ""; // constant. If no data is written, set the condition to 1 = 1, this condition is to find all the data // if you write data, query if (! Empty ($ _ POST ["name"]) // The first condition (fuzzy query is used) {$ name = $ _ POST ['name']; $ tj1 = "name like '% {$ name} %'";} if (! Empty ($ _ POST ["tel"]) {$ tel = $ _ POST ["tel"]; $ tj2 = "tel = '$ tel '";} // concatenates conditions into SQL statements $ SQL = "select * from contacts WHERE {$ tj1} AND {$ tj2 }";

:

In this way, when there are several conditions, several conditional variables are implemented. If the first condition is not empty, the first condition is executed, and the second condition is not empty, if both are empty, all data is queried.

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.