从MySQL生成下拉[关闭]

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 "<option value=\".$row['user_id'].\">.$row['user_name'].</option>
 ";
  echo "<option value=\"12275\">".$row['user_name']."</option>
 ";
}

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 '<select name="users">';

while($row = mysql_fetch_array($result)) {
    echo '<option value="'. $row['user_id'] . '">' . $row['user_name'] . "</option>
";
}

echo '</select>';

The first result in Google came up with an example...

http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html

Try to do a little research before posting in the future.