正如我在标题上所述,我想从数据库中获取数据并使它们成为HTML中的选项值。但它没有显示任何内容在我的数据库中,我有20个数据,该选项有20个值,但它都是空白的。你能告诉我哪里弄错了吗?
<?php
$query = "SELECT name_program FROM vote.program";
$result = mysql_query($query);
?>
<div class="form-group">
<label>Program</label>
<select class="form-control">
<option selected disabled>-- Select Program --</option>
<?php
while ($row = mysql_fetch_array($result)){
echo '<option value='.$row['name_program'].'</option>';
}
?>
</select>
</div>
答案 0 :(得分:1)
你写错了选项,应该是
echo '<option value="'.$row['name_program'].'">'.$row['name_program'].'</option>';
就像@icecub所说,你应该避免使用mysql_ *函数,你应该看看pdo或mysqli。
答案 1 :(得分:0)
试试这个:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT name_program FROM vote.program";
$result=mysqli_query($con,$sql);
?>
<div class="form-group">
<label>Program</label>
<select class="form-control">
<option selected disabled>-- Select Program --</option>
<?php
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo '<option value='.$row['name_program'].'>'.$row['name_program'].'</option>';
}
?>
</select>
</div>