您好我有一个用户表,我想在我的网页上显示他们的列作为选择框
下面是我的代码
$query = mysql_query("QUERY HERE");
echo '<select name="id">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['id'].'">'.$row['id'].'</option>';
}
echo '</select>';
echo '<select name="username">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['username'].'">'.$row['username'].'</option>';
}
echo '</select>';
echo '<select name="city">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['usercity'].'">'.$row['usercity'].'</option>';
}
echo '</select>';
以上代码显示下拉列表中的ID值,但用户名和用户数下拉列表显示空白
答案 0 :(得分:0)
通过你的代码,结果将仅用于第一个循环,除非你在每个循环中重复上面的查询,所以最好尝试这个:
$result = array();
$query = mysql_query("QUERY HERE");
while($row = mysql_fetch_array($query)){
$result[] = $row;
}
echo '<select name="id">';
foreach ($result as $row ){
echo '<option value="'.$row['id'].'">'.$row['id'].'</option>';
}
echo '</select>';
echo '<select name="username">';
foreach ($result as $row ){
echo '<option value="'.$row['username'].'">'.$row['username'].'</option>';
}
echo '</select>';
echo '<select name="city">';
foreach ($result as $row ){
echo '<option value="'.$row['usercity'].'">'.$row['usercity'].'</option>';
}
echo '</select>';