echo <option>,数组为其值

时间:2018-05-10 18:04:48

标签: php html

我似乎无法弄清楚如何使用mysqli_fetch_array创建的数组来使用此代码。选项值的数量是正确的,但我无法从数组中获取要显示的名称。如果我放入('')然后它会中断,因为它认为它应该停在那里。我试过逃避它们,但这给了我相同的结果。我知道如果我没有回应它它会工作但我需要这个回声,以便它只会在另一个选择值被选中时运行。

<select>

<?php 

//this is just here for testing
$GLOBALS['con'] = mysqli_connect("server", "user", "Password", "database");

$Names = mysqli_query($GLOBALS['con'], "SELECT * FROM user WHERE Department = 'IT Support'"); 
$NameSelect = mysqli_fetch_array($Names);  
$size = count($Names); 

$I=0;
while ($I <= $size) {
    echo "<option value='$NameSelect[Full_name]'>$NameSelect[Full_name]</option>";
    $I++;
}


  ?>
</select>

2 个答案:

答案 0 :(得分:2)

SELECT查询生成0到n行的结果集。为了从查询中使用结果集中的所有行,您需要遍历结果集

<?php 

//this is just here for testing
$GLOBALS['con'] = mysqli_connect("server", "user", "Password", "database");

$Names = mysqli_query($GLOBALS['con'], "SELECT * FROM user WHERE Department = 'IT Support'"); 

while ($row = mysqli_fetch_array($Names))
{  
    echo "<option value='$row[Full_name]'>$row[Full_name]</option>";
}
?>
</select>

答案 1 :(得分:0)

while ($row= mysqli_fetch_array($Names)) {
    echo "<option value='$row[Full_name]'>$row[Full_name]</option>";
}