【问题标题】:While/foreach does not fetching the value from databasewhile/foreach 不从数据库中获取值
【发布时间】:2017-09-19 12:25:47
【问题描述】:

我正在尝试从数据库中获取一些值,但是 while/foreach 不起作用。

<?php
$type_id = $article->type_id;
$sql = " SELECT * FROM `interior_image_testing` WHERE `type_id` = '$type_id' ";
//echo $sql;
//$result = mysqli_query($con,$sql);
if ($result = mysqli_query($con, $sql)) {


    //echo $result;
    $obj = mysqli_fetch_array($result);


    foreach ($obj as $objs):
        ?>

        <div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
            <img src="assets/img/<?php echo $objs['image_path']; ?> " width="100%"> <!-- needs to be changed -->
        </div> 


        <?php
    endforeach;
}
?> 

问题是,我无法从数据库中获取值。 在interior_testing 表中,我有type_idnamelocationprofile_picinterior_image_testing 表中,我有idimage_pathtype_id

在页面上,图像未获取。

【问题讨论】:

    标签: php mysqli foreach while-loop


    【解决方案1】:

    你不能在那里使用foreach。您需要反复调用mysqli_fetch_array 才能检索所有结果。一次调用一次只会检索一个结果行:

    while ($row = mysqli_fetch_array($result)) {
        echo $row['image_path'];
    }
    

    【讨论】:

      【解决方案2】:

      试试

      while($obj = mysqli_fetch_array($result)) {...}
      

      您需要循环使用 $result,而不是在第一次查询后给出的数组。在这里可以看到正确的用法;

      http://php.net/manual/en/mysqli-result.fetch-array.php

      【讨论】:

        猜你喜欢
        • 2014-10-16
        • 1970-01-01
        • 2020-09-01
        • 2019-05-23
        • 2021-09-20
        • 2017-05-04
        • 1970-01-01
        • 2015-10-13
        • 2011-10-19
        相关资源
        最近更新 更多