Multi-conditional query in PHP

Source: Internet
Author: User

The first is to query all, the steps do not detail, connect the database, query all the information in the table, the Foreach loop is printed in tabular form

Then is the form form to submit the data of the query, which is submitted to this page by post, so to determine whether there is a value in post, in determining whether the value of the post is an empty string (both cases the default user is not input, Query all) Default rules: Generally do query when: The user does not enter any content is to query all

Then the most important thing is the stitching query condition

Key code:

$TJ 1 = "1=1"; $TJ 2 = "1=1"; $name = "";        if (!empty ($_post)) {if ($_post["name"]! = ") {$name = $_post[" name "];    $TJ 1 = "name like '%{$name}% '";    if ($_post["brand"]! = ") {$TJ 2 =" brand = ' {$_post[' brand '} ' "; }} $TJ = "where". $tj 1. "and". $TJ 2; $sql = "SELECT * from car". $tj; Complete Example:<! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Multi-conditional query in PHP

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.