来自mysql的多个下拉列表

时间:2015-09-26 17:47:00

标签: php mysql

我想从我的mysql数据库中提取多个数据下拉列表。我想要4个下拉菜单。这就是我现在所拥有的:

<?php

mysql_connect('#', '#', '#');
mysql_select_db('test');

$sql = "SELECT wie, waar, metwie, voeruig FROM data";
$result = mysql_query($sql);

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

}
echo "</select>";
?>

4 个答案:

答案 0 :(得分:0)

如HawasKaPujaari所述,请避免使用mysql。使用System.arraycopy。您可以使用这样的条件切换语句:

mysqli

答案 1 :(得分:0)

例如,这个:

mysql_connect('#', '#', '#');
mysql_select_db('test');

$sql = "SELECT wie FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
    echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";

$sql = "SELECT waar FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
    echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
 }
 echo "</select>";

$sql = "SELECT metwie FROM data";
$result = mysql_query($sql);
echo "<select name='test2'>";
while ($row = mysql_fetch_array($result)){
    echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
 }
 echo "</select>";

$sql = "SELECT voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test3'>";
while ($row = mysql_fetch_array($result)){
    echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
 }
 echo "</select>";
?>

答案 2 :(得分:0)

php和mysql是不同的软件。你在这里做的是使用mysql函数将php与mysql_*()连接起来 在你的情况下你得到的是一个PHP数组。您可以将此数组用于您想要的任何内容。如果你想按照调试用途打印数组:

echo "<pre>";
print_r($row);
echo "</pre>";

从这里你将获得数组结构 然后你可以使用不同的数组元素下拉列表

注意:mysql_*()不安全。使用mysqli_*PDO

答案 3 :(得分:0)

请尝试以下代码。希望它可以帮到你

<?php
$wie=array();
$waar =array();
$metwie =array();
$voeruig =array();
while ($row = mysql_fetch_array($result)){
$wie[]=$row["wie"];
$waar[]=$row["waar"];
$metwie[]=$row["metwie"];
$voeruig[]=$row["voeruig"];

}
?> 
<select name="wie">
<?php
foreach($wie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?>
<php	

}
?>
</option>
<select name="waar">
<?php
foreach($waar as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php	

}
?>
</select>
<select name="metwie">
<?php
foreach($metwie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?></option>
<php	

}
?>
</select>
<select name="voeruig">
<?php
foreach($voeruig as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php	

}
?>
</select>