I need to generate a drop down menu from data in a MySQL table.
From the table, it'll have to take the user id and the username.
Then it'll set the user id to option value and the username to what shows up in the drop down.
Could anyone show me some code for it? I'm having trouble making the following work:
$sql = "SELECT user_id, user_name FROM users";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo ".$row['user_name'].\n ";
echo "".$row['user_name']."\n ";
}
解决方案
You have a couple of issues with your output. First, it's not clear from your example if you have the actual select element parent, and I'm not sure if browsers will display options without a parent select. Second, you are not escaping your array variables. So this might fix it:
$sql = "SELECT user_id, user_name FROM users"; $result=mysql_query($sql);
echo '';
while($row = mysql_fetch_array($result)) {
echo '' . $row['user_name'] . "\n";
}
echo '';