将一个值与数组进行比较

时间:2013-10-15 18:27:04

标签: php mysql ternary-operator

我试图将从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']是AEs的ID列表。

我无法做到这一点,因为选择菜单显示了AE的所有名称,但没有选择正确的名称。为什么?我做错了什么?

2 个答案:

答案 0 :(得分: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>";
}

答案 1 :(得分:0)

将选项行更改为:

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

我希望它很好:)