我正在创建一个个人顾问页面,在我的数据库中有3个顾问,我正在尝试创建一个下拉框,人们可以在其中选择他们想要的顾问。目前,我的下拉列表只显示了三次单词"数组"。这是我迄今为止所拥有的。
<select name="advisor">
<?
$sqlQ = "SELECT concat(firstName,' ',lastName) FROM adv WHERE advisor IS NULL";
$array=array();
$res = $db->prepare($sqlQ);
$res->execute();
echo("<option>Advisor</option>");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$array[] = $row;
}
foreach($array as $info)
{
echo("<option>$info</option>");
}
您的$row
已经是一个数组,因此无需将$行插入到新数组中。只需像这个一样循环结果
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $info)
{
echo("<option>$info</option>");
}
}
// give the result of concat() an alias so you can easily access it in the result set
$sqlQ = "SELECT concat(firstName,' ',lastName) as name FROM adv WHERE advisor IS NULL";
[...]
while ( $row = $result->fetch(PDO::FETCH_ASSOC) ) {
// $row is an array, its members correspond with the fields/aliases you've selected
// apply htmlspecialchars() so that the contents of $row['name'] can't break your html structure
echo '<option>', htmlspecialchars($row['name']), '</option>';
}