我正在创建一个个人顾问页面,在我的数据库中有3个顾问,我试图创建一个下拉框,在那里有人可以选择他们想要的顾问。目前,我的下拉列表只显示了三次单词“Array”。到目前为止我的情况是这样的。
<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>");
}发布于 2015-12-03 17:23:13
您的$row已经是一个数组,因此不需要将您的$row插入到新的数组中。只需像这样循环结果
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $info)
{
echo("<option>$info</option>");
}
}发布于 2015-12-03 17:22:30
// 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>';
}https://stackoverflow.com/questions/34072188
复制相似问题