2014-03-28 43 views
0

我有一個高級搜索表單,有三個字段。用戶可以使用任何字段或任何兩個或所有字段開始搜索。所以我想檢查用戶是否爲特定字段輸入了值,並根據通過一系列if循環提供的值來查詢查詢。但我很想知道在mysql中是否有任何equvailent技術來避免if循環並在單個查詢中實現搜索。高級搜索3字段,需要查詢

以下是檢查用戶輸入的示例。

if($school!="" & $district1!="" & $scl_type!="") 
{ 
    echo "3 fields available"; 
} 
else if($school=="" & $district1!="" & $scl_type!="") 
{ 
    echo "school empty, district,schooltype available"; 
} 
else if($school!="" & $district1=="" & $scl_type!="") 
{ 
    echo "school,school type available district empty"; 
} 
else if($school!="" & $district1!="" & $scl_type=="") 
{ 
    echo "school,district only present,school type is empty"; 
} 

.....

所以我在每個if語句編寫不同的查詢。有沒有捷徑?

< ----更新:---> 我得到了這個查詢,它工作正常。但是當同一地區有兩所學校只有一所學校可以返回時。它只返回一個結果,甚至很多結果可用,該怎麼辦?

SELECT * FROM `register` WHERE 
    (schoolname IS NULL OR schoolname LIKE '%national%') 
      AND 
    (schooltype IS NULL OR schooltype LIKE '%state board%') 
      AND 
    (district IS NULL OR district LIKE '%thiruvarur%'); 
+1

可能重複http://stackoverflow.com/questions/12600427/php-mysql-function-with-optional-parameter) –

回答

1

你的參數 - @學校,@ schooltype,@區

select items from yourtable where 
    (school like @school) 
     and 
    (schooltype like @schooltype) 
     and 
    (district like @district) 

同時傳遞參數,如果任何字段爲空,作出相應的參數= '%%',對於任何輸入字段,使相應的參數=「'」+ enterdfieldtext +「'」

例如:在第二種情況下,查詢將看起來像

select * from table where 
    (school like '%%') 
     and 
    (schooltype like 'givenschooltype') 
     and 
    (district like 'givendistrict') 
0

認沽條件變量一樣

if($school!="" & $district1!="" & $scl_type!="") 
{ 
    $condition = "your condition"; 
} 
else if($school=="" & $district1!="" & $scl_type!="") 
{ 
    $condition = "your condition"; 
} 
else if($school!="" & $district1=="" & $scl_type!="") 
{ 
    $condition = "your condition"; 
} 
else if($school!="" & $district1!="" & $scl_type=="") 
{ 
    $condition = "your condition"; 
} 

Write your single Query here 
2

你試試這個

$where = array(); 
if($school) 
{ 
    $where[] = "school where condition"; 
} 
if($district1) 
{ 
    $where[] = "district1 where condition"; 
} 
if($scl_type) 
{ 
    $where[] = "scl_type where condition"; 
} 
$where = implode(' and ',$where); 

$query = "select * from tablename where ".$where; 
0

您可能感興趣的三元聲明:

$condition1=$school===""?"1=1":"school=$school"; 
    $condition2=$district===""?"1=1":"district=$district"; 
    ...Etc 

    SELECT * FROM database WHERE $condition1 AND $condition2 AND $etc... 

Bascially,三元聲明等於對此:

if($school===""){ 
     $condition1="1=1"; 
    } else { 
     $condition1="school=$school"; 
    } 

但在一行中。您可以遍歷每個條件並相應地設置變量,然後在查詢中將它們全部組合在一起,然後創建一個查詢。

您將在COURSE之前在查詢之前實施某種注入保護 - 例如在分配變量時轉義變量$school等等。

0

謝謝你們給我的答案..

SELECT * FROM `register` WHERE 
     (schoolname IS NULL OR schoolname LIKE '%national%') 
       AND 
     (schooltype IS NULL OR schooltype LIKE '%state board%') 
       AND 
     (district IS NULL OR district LIKE '%thiruvarur%'); 
+0

我現在有另外一個問題,這個查詢可以正常工作,但是當兩個學校在同一地區可用時,它只會回退一所學校。該怎麼辦? – Vignesh

1

查詢取決於你的數據庫結構,但腳本可以是這樣的:

$data = filter_input_array(
    INPUT_GET, 
    array(
     'street' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, 
     'district' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, 
     'scl_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS 
    ) 
); 

if($data){ 
    $condition = array(); 
    foreach($data as $key => $value){ 
     $condition[] = sprintf("`%s` = '%s'", $key, $value); 
    } 

    $SQL_QUERY = sprintf('SELECT * FROM database_table WHERE %s', implode(' AND ', $condition)); 
} 
[可選參數PHP的MySQL的函數(的