且构网

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

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

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

要做到这一点,您想循环遍历您的查询结果并将这些信息用于您的每个下拉选项。您应该可以很容易地调整下面的代码以满足您的需求。

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