使用php - bootstrap样式将数据从数据库检索到选定的框

时间:2016-06-17 07:51:19

标签: php mysql twitter-bootstrap

我设计了一个带引导程序的表单,我想从数据库中检索数据到选择框。我试过这段代码:

<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个选项,但空白选项,如没有文字。

1 个答案:

答案 0 :(得分:0)

使用mysqli_query代替mysql_querymysqli_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>";
}