且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 MySQL 数据库中获取数据到 html 下拉列表

更新时间:2023-10-08 10:48:10

为此,您需要遍历查询结果的每一行,并将此信息用于每个下拉选项.您应该能够很容易地调整下面的代码以满足您的需要.

To do this you want to loop through each row of your query results and use this info for each of your drop down's options. You should be able to adjust the code below fairly easily to meet your needs.

// Assume $db is a PDO object
$query = $db->query("YOUR QUERY HERE"); // Run your query

echo '<select name="DROP DOWN NAME">'; // Open your drop down box

// Loop through the query results, outputing the options one by one
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
   echo '<option value="'.$row['something'].'">'.$row['something'].'</option>';
}

echo '</select>';// Close your drop down box