PHP implements multi-condition query of instance code. Query File (search. php) 1. generate a query statement: Copy the code as follows :? $ Connmysql_connect (localhost, root,); $ dbmysql_select_db (lingyun); $ queryselect * fr Query File (search. php)
1. generate a query statement:
The code is as follows:
$ 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:
The code is as follows:
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
& Lt;/td>";
}
$ 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" ";
?>
Compile (search. php) 1. generate a query statement: the code is as follows :? $ Conn = mysql_connect ("localhost", "root", ""); $ db = mysql_select_db ("lingyun"); $ query = "select * fr...