2011-10-25 26 views
0

如果我刪除「and table.field ='$ filter'OR table.field ='$ otherfilter'」,以下查詢正常工作。如果我添加這部分,那麼它會顯示我無權查看的人的結果。我明確需要添加該部分,以便顯示過濾結果。如何組織這個所以它顯示我用正確的結果:與table.field = '$過濾器' OR table.field = '$ otherfilter'爲什麼這個查詢忽略了過濾器?

代碼:

//$filter = "23943409" 
    $filter2 = "$username"; 


    $sql = "SELECT table.field, table2.field, table.field, table.field, 
    table.field, table.field, table.field FROM table 
    LEFT JOIN table2 ON table.field = table2.field 
    WHERE table.field='$id' and table.field='$id' 
    and table.field = '$filter' OR table.field='$otherfilter' 
    ORDER BY table.id DESC LIMIT 0, 3"; 

    $result=mysql_query($sql); 

    $query = mysql_query($sql) or die ("Error: ".mysql_error()); 

    if ($result == "") 
    { 
    echo ""; 
    } 
    echo ""; 


    $rows = mysql_num_rows($result); 

    if($rows == 0) 
    { 
    print(""); 

} 
elseif($rows > 0) 
{ 
while($row = mysql_fetch_array($query)) 
{ 

$field = $row['field']; 

print("$field"); 
} 

} 
+1

您註釋掉了$ filter,所以此變量爲空。因此,您的查詢變成:和table.field ='' – bart

回答

1

嘗試AND (table.field = '$filter' OR table.field = '$otherfilter')。注意該子句的括號。我假設你已經混淆了代碼,並用「field」替換了實際的字段名稱。

+0

有趣的是,它可以在其他地方正常工作,但不會冒險。將你的建議添加到evertyhwere我使用這個。 – AAA

1

你必須把你的左右括號的過濾器:

AND (table.field = '$filter' OR table.field='$otherfilter') 

否則你的邏輯是關閉的。

1

你在說「字段是一個蘋果AND字段是橙色的」與table.field=$id AND table.field = $filter。希望這只是對模糊你的表/字段名而言的懶惰,但是如果你對這兩個值使用相同的table.field組合,那麼你將排除所有記錄,因爲單個記錄字段的值不能超過一個一次。

+0

只是混淆。 – AAA

1

您可能在處理andor之間的優先級問題。嘗試:

"SELECT table.field, table2.field, table.field, table.field, 
    table.field, table.field, table.field FROM table 
    LEFT JOIN table2 ON table.field = table2.field 
    WHERE table.field='$id' and table.field='$id' 
    and (table.field = '$filter' OR table.field='$otherfilter') 
    ORDER BY table.id DESC LIMIT 0, 3 
相關問題