2012-07-25 63 views
0

我在查詢時排除我的「軟刪除」列時出現問題我已將以下代碼附加到了我的代碼之下,以獲取此工作的任何提示?它表明,具有deleted = 1行和我需要它只是爲了顯示0在查詢過程中不排除軟刪除行

$query=mysql_real_escape_string($_GET['query']); 

$query_for_result=mysql_query("SELECT * FROM Assets WHERE 
`Badge` like '%".$query."%' or 
`Status` like '%".$query."%' or 
`First Name` like '%".$query."%' or 
`Last Name` like '%".$query."%' or 
`Service Tag` like '%".$query."%' or 
`Asset Tag` like '%".$query."%' and 
`deleted` = '0' ")or die(mysql_error()); 



if(mysql_num_rows($query_for_result1)==0){ 

} 

while($data_fetch=mysql_fetch_array($query_for_result)) 
{ 

echo '<h3>';  
echo "<a href=\"modify.php?id=" . $data_fetch['id'] . "\">" . $data_fetch['First Name'] . ' ' . $data_fetch['Last Name'] . "</a>"; 
echo '<br/><b>Badge:</b> '. $data_fetch['Badge']; 
echo '<br/> <b>Service Tag:</b> '. $data_fetch['Service Tag']; 
echo ' <b>Asset Tag:</b> '. $data_fetch['Asset Tag']; 
echo '<br/> <b>Status:</b> '. $data_fetch['Status']; 
echo '<br/><b>Employee Status: </b>'; 
    if($data_fetch['Employee Status'] == 'Active'){ 
echo '<font color="#32CD32">' . $data_fetch['Employee Status'] . '</font>'; 
    } 
elseif($data_fetch['Employee Status'] == 'Terminated'){ 
echo '<font color="red">' . $data_fetch['Employee Status'] . '</font>';} 
echo '<br/> <b>Last Modified:</b> '. $data_fetch['Last Modified']; 
echo "<span> </span>"; 
echo '</h3>'; 
echo '<br/><p>' ; 

} 

回答

1

所有的條件是在同一範圍內。這意味着,只要你達到第一個條件,那麼整個where子句就是真實的。你需要把OR子句放在括號裏。

WHERE 
(
`Badge` like '%".$query."%' or 
`Status` like '%".$query."%' or 
`First Name` like '%".$query."%' or 
`Last Name` like '%".$query."%' or 
`Service Tag` like '%".$query."%' or 
`Asset Tag` like '%".$query."%' 
) 
and `deleted` = '0' 
+0

這個作品很完美。也感謝解釋爲什麼它沒有工作!我剛剛學習這個東西,像你這樣的職位是非常有幫助的:) – user1548769 2012-07-25 17:16:34