An example of a simple multi-condition query

Source: Internet
Author: User
In our website design process, multi-condition queries are often used. the source code of this article is an example of a second-hand house query. In this example, we need to obtain the information required by the customer through multiple conditions, such as geographical location, property type, house price, house area, and information release date. The following is the implementation process. Query condition interface (omitted): search. php often uses multi-condition query during the design of our website. The source code of this article is an example of a second-hand house query. In this example, we need to obtain the information required by the customer through multiple conditions, such as geographical location, property type, house price, house area, and information release date. The following is the implementation process.
Query condition interface (omitted ):
Search. php)
1. generate a query statement:
$ Conn = mysql_connect ("localhost", "root ","");
$ Db = mysql_select_db ("lingyun ");
$ Query = "select * from message where tradetype = '". $ tradetype. "'"; // transaction type, such as rent or sale
$ SQL = $ SQL. "wuye = '". $ wuye ."'";
If ($ housetype! = "Unlimited "){
$ Query. = "& housetype = '". $ housetype. "'"; // house type, such as Room 2, Room 1, Room 3, and room 2
}
If ($ degree! = "Unlimited "){
$ Query. = "& degree = '". $ degree. "'"; // New or old
}
If ($ wuye! = "Unlimited "){
$ Query. = "& wuye = '". $ wuye. "'"; // property types such as housing and shops
}
If ($ price2! = ""){
Switch ($ price1 ){
Case "greater ":
$ Query. = "& price> '". $ price2. "'"; // price
Break;
Case "equal ":
$ Query. = "& price = '". $ price2 ."'";
Break;
Case "less ":
$ Query. = "& price <'". $ price2 ."'";
Break;
}
}
If ($ area2! = ""){
Switch ($ area1 ){
Case "greater ":
$ Query. = "& area> '". $ area2. "'"; // area
Break;
Case "equal ":
$ Query. = "& area = '". $ area2 ."'";
Break;
Case "less ":
$ Query. = "& area <'". $ area2 ."'";
Break;
}
}
Switch ($ pubdate) {// release date
Case "within this week ":
$ Query. = "& TO_DAYS (NOW ()-TO_DAYS (date) <= 7 ";
Break;
Case "within one month ":
$ 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;
}
?>

II. output query results:
If ($ page ){
$ Page_size = 20;
$ Result = mysql_query ($ query );
# $ Message_count = mysql_result ($ result, 0, "total ");
$ Message_count = 10;
$ Page_count = ceil ($ message_count/$ page_size );
$ Offset = ($ page-1) * $ page_size;
$ Query = $ query. "order by date desc limit $ offset, $ page_size ";
$ Result = mysql_query ($ query );
If ($ result ){
$ Rows = mysql_num_rows ($ result );
If ($ rows! = 0 ){
While ($ myrow = mysql_fetch_array ($ result )){
Echo"";
Echo"";
Echo"$ Myrow [id] $ myrow [tradetype] $ myrow [address] $ myrow [wuye] ($ myrow [housetype]) [$ myrow [date] ";
Echo"";
Echo"Details";
Echo"";
}
}
Else echo"



No record meeting your conditions found

";
}

$ Prev_page = $ page-1;
$ Next_page = $ page 1;
Echo"

";
Echo "no.". $ page. "/". $ page_count. "page ";
If ($ page <= 1 ){
Echo "| page 1 | ";
}
Else {
Echo "| page 1 | ";
}
Echo "";
If ($ prev_page <1 ){
Echo "| previous page | ";
}
Else {
Echo "| previous page | ";
}
Echo "";
If ($ next_page> $ page_count ){
Echo "| next page | ";
}
Else {
Echo "| next page | ";
}
Echo "";
If ($ page >=$ page_count ){
Echo "| last page | ";
}
Else {
Echo "| last page | ";
}
Echo"

";
}
Else {
Echo"

No rental information is available now!

";
}
Echo" ";
?>


If any problem occurs during use, please contact me (yk_lingyun@21cn.com), please correct me!


[This article is copyrighted by the author and osuo. if you need to reprint it, please indicate the author and its source]

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.