An example of a simple implementation of a multiple-condition query
Source: Internet
Author: User
In our website design process, often use a multiple conditions query, the source of this article is a second-hand housing query example. In this case, we need to be able to find the information we need through our geographical location, property type, house price, house area and information release date. The following is the implementation process.
Query Criteria Interface (abbreviated):
Query file (search.php)
First, generate query statements:
?
$conn =mysql_connect ("localhost", "root", "");
$db =mysql_select_db ("Lingyun");
$query = "SELECT * from message where tradetype= '". $tradetype. "'"; Type of transaction, such as renting, selling
$SQL = $SQL. "Wuye= '". $wuye. "'";
if ($housetype!= "unlimited") {
$query. = "&& housetype= '". $housetype. "'"; Type of house, such as two bedroom, one hall, three rooms and two halls
}
if ($degree!= "unlimited") {
$query. = "&& degree= '". $degree. "'"; New and Old degree
}
if ($wuye!= "unlimited") {
$query. = "&& wuye= '". $wuye. "'"; Property types such as housing, shops
}
if ($price 2!= "") {
Switch ($price 1) {
Case "Greater than":
$query. = "&& price> '". $price 2. "'"; Price
Break
Case "equals":
$query. = "&& price= '". $price 2. "'";
Break
Case "less than":
$query. = "&& price< '". $price 2. "'";
Break
}
}
if ($area 2!= "") {
Switch ($area 1) {
Case "Greater than":
$query. = "&& area> '". $area 2. "'"; Area
Break
Case "equals":
$query. = "&& area= '". $area 2. "'";
Break
Case "less than":
$query. = "&& area< '". $area 2. "'";
Break
}
}
Switch ($pubdate) {//release date
Case "Within this week":
$query. = "&& to_days (Now ())-to_days (date) <=7";
Break
Case "within one months":
$query. = "&& to_days (Now ())-to_days (date) <=30";
Break
Case "within three months":
$query. = "&& to_days (Now ())-to_days (date) <=91";
Break
Case "Within six months":
$query. = "&& to_days (Now ())-to_days (date) <=183";
Break
}
if ($address!= "") {
$query. = "&& address like '% $address% '"; Address
}
if (! $page) {
$page = 1;
}
?>
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