dongyakui8675 2016-11-08 14:54
浏览 15

MySQL查询显然返回额外的行

While retrieving the data from MySQL through PHP, it retrieves one extra row which is NOT present in database. Why?

<?php

$con = mysqli_connect('host', 'username', 'password', 'database');
$sql = $con->query("SELECT * FROM info");

while($row = $sql->fetch_array()) {
    $image = $row['image_path'];
    $caption = $row['caption'];
    $id = $row['id'];
    ?>
    <div class="col-md-4">
        <div class="thumbnail">
            <img src=<?php echo $image;?>>
            <div class="caption text-center"><h3><?php echo $caption;?></h3></div>
            <button class="btn btn-default" onclick="modal(<?php  echo $id;?>)">View</button>
        </div>
    </div><!-- Column END -->
    <?php
    }
?>
  • 写回答

1条回答 默认 最新

  • doujiao3074 2016-11-08 15:34
    关注

    Its because you are using

    $row = $sql->fetch_array();

    which defaults to

    $row = $sql->fetch_array(MYSQLI_BOTH);

    and returns a NULL value at the end of the result set because of the MYSQLI_ROW parameter which is included in MYSQLI_BOTH

    You can do this to fix it:

    while ($row = $sql->fetch_array(MYSQLI_ASSOC)) {

    评论

报告相同问题?