从 PHP 中的 mySQL 表填充下拉框 [英] Populate a Drop down box from a mySQL table in PHP
问题描述
我正在尝试在 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 数据库,发出您的查询,并为表中每一行的 框输出
标记.
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屋!