使用MySQL查询结果填充下拉列表(PHP/MySQL) [英] Populate dropdown with MySQL query results (PHP/MySQL)

查看:193
本文介绍了使用MySQL查询结果填充下拉列表(PHP/MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述.....

so as the title says.....

这是我当前编写的代码,它认为它可以工作,但不起作用:(

here is the code I currently wrote thinking it would work and it doesnt :(

请注意我的会话用户ID等正在工作,因为我可以将它打印在表单的另一个字段中,所以这不是问题,但是我的保管箱似乎什么都没有. (我已经在与我登录的user_id匹配的数据库上创建了数据)

note my session userid etc is working as I can get it to print out in another field in the form so thats not the problem, but my dropbox just seems to have nothing in it. (i have created the data on the database with the user_id matching of which I am logged in with)

$userid = $_SESSION['myuserid'];
//run query to database
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($query))
    {
    $dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
    } 

然后在html中使用它:

this is then used in the html:

<select name="t_group"><? echo $dd; ?></select>

有人可以帮我吗?

谢谢

推荐答案

$query是一个字符串,因此无法从中获取任何结果.您应该执行以下操作:

$query is a string and therefore you cannot get any results from it. You should do something like:

$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    $dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
} 

这篇关于使用MySQL查询结果填充下拉列表(PHP/MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆