Php+mysql database Condition query and multi-condition query Instance code

Source: Internet
Author: User
This article to you to share is the use of PHP to implement single conditions and multi-conditional query code and examples, very practical, the need for small partners can refer to the following

Single-Condition query:

1. First, you need to have a table showing 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 watch that doesn't change anything.

2. Another from form, let the user input, click Query:

<form action= "shouye.php" method= "POST" >  <p>    input name: <input type= "text" name= "name"/>    <input type= "Submit" value= "inquiry"/>  </p></form>

3. Establish keyword query:

<?php//Implements two logical//1. If there is no post data. Check all the//2. If there is a post data. Check $db with conditions = new mysqli ("localhost", "root", "12345678", "Heiheihei")  ;  Connect database $TJ = "1 = 1";  $name = "";    Heng Set, if not write data, then let the condition equals 1=1, this condition is to find all the data//If you write data, according to the data check if (!empty ($_post)) {$name = $_post[' name '];  $TJ = "name like '%{$name}% '";  }//stitching the condition into the SQL statement $sql = "SELECT * from Contacts WHERE {$TJ}";  Echo $sql;  Find out $r = $db->query ($sql); Pass value if ($R)//Start judging {//$attr has received the value, now just need to get his index on the line while ($attr = $r->fetch_row ()) {//keyword Special query $  str = str_replace ($name, "<mark>{$name}</mark>", $attr [1]);     Find replacements such as Ctrl+f//substr_replace ();    Replace//substr () at the specified location;      Intercept string $gname = "Select Gname from Groups WHERE gid= ' {$attr [3]} '";      The GID in the Grouping table, and the $nresult i clicked = $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-Criteria Query:

As usual in front;

Out of the PHP statement:

<?php  //Implements two logical  //1. If there is no post data. Check all the  //2. If you have post data. Check  $db = new mysqli ("localhost", "root", " 12345678 "," Heiheihei ");  Connect the database  $tj 1 = "1 = 1";  $TJ 2 = "1 = 1";//two conditions of identity  $name = "";  Heng Set, if not write the data, then let the condition equals 1=1, this condition is to find all the data  //If you write data, according to the data check if  (!empty ($_post["name"))//First condition judgment (using fuzzy query)  {    $name = $_post[' name '];    $TJ 1 = "name like '%{$name}% '";  }  if (!empty ($_post["Tel"]))  {    $tel = $_post["Tel"];    $TJ 2 = "Tel = ' $tel '";  }  Stitching conditions into the SQL statement  $sql = "SELECT * from Contacts WHERE {$TJ 1} and {$TJ 2}";

This way: There are a few conditions to do a few condition variables, the first condition is not empty to execute the first condition, the second condition is not empty execution of the second condition, two is empty is to search all the data

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.