PHP instance: simple multi-condition query using PHP

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 materials required by the customer through the geographical position, property type, house price, house area and information announcement date.

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 materials required by the customer through the geographical position, property type, house price, house area and information announcement date.

Search. php)

I. native 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 exhibition
}
If ($ price2! = ''){
Switch ($ price1 ){
Case 'day ':
$ Query. = '& price> ''. $ price2. '''; // price
Break;
Case 'is ':
$ Query. = '& price = ''. $ price2 .''';
Break;
Case '< ':
$ Query. = '& price <''. $ price2 .''';
Break;
}
}
If ($ area2! = ''){
Switch ($ area1 ){
Case 'day ':
$ Query. = '& area> ''. $ area2. '''; // area
Break;
Case 'is ':
$ Query. = '& area = ''. $ area2 .''';
Break;
Case '< ':
$ Query. = '& area <''. $ area2 .''';
Break;
}
}
Switch ($ pubdate) {// announce the date
Case 'past week ':
$ Query. = '& TO_DAYS (NOW ()-TO_DAYS (date) <= 7 ';
Break;
Case 'within a month ':
$ Query. = '& TO_DAYS (NOW ()-TO_DAYS (date) <= 30 ';
Break;
Case 'within three month ':
$ Query. = '& TO_DAYS (NOW ()-TO_DAYS (date) <= 91 ';
Break;
Case 'within six month ':
$ 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 'Page. $ page. '/'. $ page_count ';
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' ';
?>


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.