2013-10-15 143 views
1

我想比較一個從mysql中取出的值與從mysql中取出的數組的比較。代碼如下所示:比較一個值與一個數組

$aeSelect = "SELECT id, firstName, lastName from user WHERE fiID = 0 AND access = 3 ORDER BY lastName"; 
$aeQuery = mysql_query($aeSelect) or die ("Could not get AE list: " . mysql_error()); 
while ($rowAE = mysql_fetch_array($aeQuery)) { 
    echo "<option " . (($row['aeid'] == $rowAE['id']) ? "selected='selected'" : "") . ">" . $rowAE['firstName'] . " " . $rowAE['lastName'] . "</option>"; 
} 

$row['aeid']是20022和$rowAE['id']是不良的ID的列表。

我不能這樣做,因爲選擇菜單顯示所有AE的名稱,但沒有選擇正確的名稱。爲什麼?我究竟做錯了什麼?

+1

如何變$行已初始化? –

回答

0

首先,我只想說,在今天這個時代,在PHP中使用像Yii或Cake這樣的框架來訪問數據庫是非常愚蠢的,除非這是一個非常小的站點。

說了這麼多,您所遇到的問題是一個簡單的解決辦法:

$aeSelect = "SELECT id, firstName, lastName from user WHERE fiID = 0 AND access = 3 ORDER BY lastName"; 
$aeQuery = mysql_query($aeSelect) or die ("Could not get AE list: " . mysql_error()); 
while ($rowAE = mysql_fetch_array($aeQuery)) { 
    echo "<option " . (($row['aeid'] == $rowAE['id']) ? "selected='selected'" : "") . ' value="'. $rowAE['id']. "\">" . $rowAE['firstName'] . " " . $rowAE['lastName'] . "</option>"; 
} 
0

的變化選項一行到這一點:

echo "<option "; 
if ($row['aeid'] == $rowAE['id']) echo 'selected="selected"'; 
echo ' value="'.$rowAE['id'].'">' . $rowAE['firstName'] . ' '. $rowAE['lastName'] . '</option>'; 

我希望這是好的:)

相關問題