从PHP中的mySQL表填充下拉框 [英] Populate a Drop down box from a mySQL table in PHP

查看:63
本文介绍了从PHP中的mySQL表填充下拉框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PHP中的mySQL查询结果中填充一个下拉框.我在网上查找了示例,并在我的网页上进行了尝试,但是由于某些原因,它们根本没有填充我的下拉框.我已经尝试调试代码,但是在我看过的网站上并没有真正对其进行解释,因此我无法弄清楚每一行代码是什么.任何帮助都会很棒:)

I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)

这是我的查询:Select PcID from PC;

推荐答案

您需要确保如果使用的是WAMP之类的测试环境,请将用户名设置为root. 这是一个示例,该示例连接到MySQL数据库,发出查询,并从表的每一行中为<select>框输出<option>标记.

You will need to make sure that if you're using a test environment like WAMP set your username as root. Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.

<?php

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);

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

?>

这篇关于从PHP中的mySQL表填充下拉框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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