代码之家  ›  专栏  ›  技术社区  ›  user8579791

While/foreach不从数据库中获取值

  •  0
  • user8579791  · 技术社区  · 8 年前

    我试图从数据库中获取一些值,但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_id , name location profile_pic interior_image_testing 我有桌子 id , image_path .

    在页面上,图像未被提取。

    2 回复  |  直到 8 年前
        1
  •  0
  •   poke    8 年前

    您不能使用 foreach mysqli_fetch_array 以检索所有结果。一个调用只会一次检索一个结果行:

    while ($row = mysqli_fetch_array($result)) {
        echo $row['image_path'];
    }
    
        2
  •  0
  •   ag0702    8 年前

    尝试

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

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