2012-07-06 44 views
0

我有一個選擇框,其中包含所有選項,然後是用戶列表。如果/ else if/else查詢數據庫導致false。

我正在努力創造的東西是這樣的。除了試圖查詢數據庫來檢查變量是否在數據庫中,我大部分都是這樣。

if ($variable == 'All') { code here } 

    else if ($variable != 'ALL' != *[result in database]*) { code here } 

    else { code here } 

我除了試圖查詢數據庫,以檢查它是否在數據庫中的大部分。

任何建議如何我可以在我的if語句中組合一個mySQL數據庫的查詢。

謝謝

+0

很難說沒有看到您的查詢,但你不能只添加一個'WHERE'條件,如果你的'$變量! ='All'' – jeroen 2012-07-06 20:46:59

回答

2
if ($variable == 'All') { 
... do something ... 
} else { 
    $sql = "SELECT ..."; 
    $result = mysql_query($sql) or die(mysql_error()); 
    $row = mysql_fetch_assoc($result); 
    if ($row['somefield'] == 'whatever') { 
     ... do something else ... 
    } else { 
     ... do something even "elser" ... 
    } 
} 
+0

乾杯這有助於,謝謝! – cbarlow123 2012-07-06 20:52:37

1

你不能使用那樣的操作符。

替換:

else if ($variable != 'ALL' != *[result in database]*) { code here } 

有了:

else if ($variable != 'ALL' AND $variable != *[result in database]*) { code here }