2014-10-09 80 views
0

我有一個網站搜索表單,並且希望用戶有幾個搜索詞輸入到執行數據庫搜索,條款如下:PHP MySQL的搜索與多個標準

  • 關鍵詞
  • 樓盤(出售,出租...)
  • 物業類型(公寓,排屋...)
  • 國家
  • 最低報價
  • 最高價格

這裏是腳本來執行以上期限的輸入

public function get_property_list_by_search($start, $per_page, $keyword, $prop_for, $min, $state, $ptype, $max, $mysqli) 
{ 
    if(empty($start) && empty($per_page)) 
    { 
     return 0; 
    } 

    $start = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($start)); 
    $per_page = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($per_page)); 
    $keyword = $mysqli->real_escape_string(stripslashes($keyword)); 
    $prop_for = $mysqli->real_escape_string(stripslashes($prop_for)); 
    $state = $mysqli->real_escape_string(stripslashes($state)); 
    $ptype = $mysqli->real_escape_string(stripslashes($ptype)); 
    $min_price = self::num_clean($mysqli->real_escape_string($min)); 
    $max_price = self::num_clean($mysqli->real_escape_string($max)); 

    $t1 = ''; 
    $t2 = ''; 
    $t3 = ''; 
    $t4 = ''; 
    $t5 = ''; 

    if(isset($keyword) && !empty($keyword)){ 
     $t1 = " AND `proj_title` LIKE '%".$keyword."%' OR `proj_addr` LIKE '%".$keyword."%' OR `proj_area` LIKE '%".$keyword."%'"; 
    } 
    if(isset($prop_for) && !empty($prop_for)){ 
     $t2 = " AND `proj_for`='".$prop_for."'"; 
    } 
    if(isset($state) && !empty($state)){ 
     $t3 = " AND `state`='".$state."'"; 
    } 
    if(isset($ptype) && !empty($ptype)){ 
     $t4 = " AND `proj_cat`='".$ptype."'"; 
    } 
    //min & max 
    if((isset($min_price) && !empty($min_price)) && (isset($max_price) && !empty($max_price))){ 
     $t5 = " AND `price` BETWEEN '".$min_price."' AND '".$max_price."'"; 
    } 
    //min only 
    if(!empty($min_price) && empty($max_price)){ 
     $t5 = " AND `price` >= '".$min_price."'"; 
    } 
    //max only 
    if(empty($min_price) && !empty($max_price)){ 
     $t5 = " AND `price` <= '".$max_price."'"; 
    } 

    $sql = $mysqli->query("SELECT * FROM `project` WHERE `status`='1' ". 
    $t1." ".$t2." ".$t3." ".$t4." ".$t5." ". 
    "ORDER BY `posted_date` DESC LIMIT ".$start.", ".$per_page); 

    if($sql->num_rows > 0){ 
     return $sql; 
    }else{ 
     return false; 
    } 
} 

搜索查詢輸出將是這樣的:

SELECT * FROM `project` 
WHERE `proj_title` LIKE '%keywords%' 
OR `proj_addr` LIKE '%keywords%' 
OR `proj_area` LIKE '%keywords%' 
AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000' 

(數據類型priceDECIMAL(10,2),它儲值像250000.00

但是,返回的結果不像預期的那樣(不準確),它也會以超過60萬的價格和超出「8」的項目類別出現,這是最終用戶在網站上搜索不到的。

有沒有什麼辦法來改進查詢執行更具體的?

回答

1

而不是採取這些變量,你應該使用「。=」運算符。

/*  $t1 = ''; 
     $t2 = ''; 
     $t3 = ''; 
     $t4 = ''; 
     $t5 = ''; 
*/ 
     $q = "SELECT * FROM `property` WHERE `status`='1' "; 

// You need to enclose all **OR** logical tests in parenthesis. 
// Moreover most of the usages of isset function are useless, 
// as your are initializing many variables 

     if($keyword && !empty($keyword)){ 
      $q .= " AND (`p_title` LIKE '%".$keyword."%' OR `address` LIKE '%".$keyword."%' OR `area` LIKE '%".$keyword."%')"; 
     } 
     if($prop_for && !empty($prop_for)){ 
// If you are using double quotes you really don't need handle to concatenation. 
     $q .= " AND `p_for`='$prop_for'"; 
     } 
     if($state && !empty($state)){ 
      $q .= " AND `state`='$state'"; 
     } 
     if($ptype && !empty($ptype)){ 
      $q .= " AND `p_category`='$ptype'"; 
     } 
     //min only 
     if($min_price && !empty($min_price)){ 
      $q .= " AND `price` >= '".$min_price."'"; 
     } 
     //max only 
     if($max_price && !empty($max_price)){ 
      $q .= " AND `price` <= '$max_price'"; 
     } 

// When you are not using OFFSET keyword, 
//the first number after LIMIT keyword should be the number of records 
       $q .= " ORDER BY `posted_date` DESC LIMIT $per_page , $start;"; 
       $sql = $mysqli->query($q); 
+0

感謝的人。我可以向你學習嗎?我怎樣才能輸出一個查詢來檢查課堂的目的?我仍然不知道導致失敗的查詢出了什麼問題。 – conmen 2014-10-09 08:22:50

+0

哦,確定@conmen,:)你可以加我FB(fb.com/gahse)。我只是要求你添加 echo $ q; 在這行之前 $ sql = $ mysqli-> query($ q); 這將返回查詢字符串,然後您可以複製此字符串並將其粘貼到任何MySql客戶端,如phpmyadmin或sqlyog。它將幫助您刪除所有語法錯誤。 – gahse 2014-10-09 08:45:18

1

你將需要括號。

SELECT * FROM `project` WHERE (`proj_title` LIKE '%keywords%' OR `proj_addr` LIKE '%keywords%' OR `proj_area` LIKE '%keywords%') AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000' 

沒有括號,它只需要匹配最後一個OR之前的條件之一。

+0

打我吧.. – Devon 2014-10-09 04:24:17

0
if(isset($_SESSION['login'])) 
{ 
echo "<div align=\"right\"><strong><a href=\"index.php\"> Home </a>| 
<a href=\"signout.php\">Signout</a>| 
<a href=\"home.php\">Profile</a></strong></div>"; 


} 
else 
{ 
    echo "&nbsp;"; 
} 

$con= mysql_connect("localhost","root",""); 
    $d=mysql_select_db("matrimonial",$con); 
    $gender=$_POST['gender']; 
    $age1=$_POST['age1']; 
    $age2=$_POST['age2']; 
    $city=$_POST['city']; 
    $subcast=$_POST['subcast']; 
    $result=mysql_query("select * from matri where gender='$gender' and age between '$age1' and '$age2' and city='$city' and subcast='$subcast'"); 

if($gender && !empty($gender)) 
{ 
$result .= " AND `gender`='$gender'"; 
} 

if($age1 && !empty($age1)){ 
     $result .= " AND `age`='$age1'"; 
    } 
if($age2 && !empty($age2)){ 
     $result .= " AND `age`='$age2'"; 
    } 

if($city && !empty($city)){ 
     $result .= " AND `city`='$city'"; 
    } 

    if($subcast && !empty($subcast)){ 
     $result .= " AND `subcast`='$subcast'"; 
    } 

    $result .= " select * from "; 

    $sql = $mysql->query($result); 
how to run this code 
+0

我想搜索此字段選擇或不選擇用戶。如果沒有選擇比所有其他明智的搜索條件,我做什麼 – 2015-05-14 06:21:20