我设计了一个带引导程序的表单,我想从数据库中检索数据到选择框。我试过这段代码:
<div class="form-group"><label class="col-sm-2 control-label">Location</label>
<div class="col-sm-8"><select class="form-control m-b" name=Location>
<?php
$mysqli = new mysqli( 'localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db' );
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM adhoc";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['Aim'] . "></option>";
}
?>
</select>
</div>
</div>
现在它从数据库中检索选定框中的数据,就像我有2行一样;它显示2个选项,但空白选项,如没有文字。
答案 0 :(得分:0)
使用mysqli_query
代替mysql_query
和mysqli_fetch_assoc
代替mysql_fetch_array
$query = "SELECT * FROM adhoc";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['Aim'] . "></option>";
}