而 fetch() 下的循环只打印第一行


while loop under fetch() prints the first row only

if (isset($_GET['srId'])) {
    $srId = $_GET['srId'];
}
$getShowroomDetails = $db->prepare('SELECT
a.id, a.roomName, a.roomImage, a.roomDetails,
b.id, b.showroom, b.activityName, b.activityPlace, b.activityDate,b.activityDetails,b.activityImage
FROM movies_showroom AS a
INNER JOIN movies_showroom_details AS b ON (a.id = b.showroom)
WHERE a.id=?
');
$getShowroomDetails->bind_param('i', $srId);
if ($getShowroomDetails->execute()) {
    mysqli_stmt_bind_result($getShowroomDetails, $id, $roomName, $roomImage, $roomDetails, $showroom, $sid, $activityName, $activityPlace, $activityDate, $activityDetails, $activityImage);
    $getShowroomDetails->fetch();
}
    ?>

现在在HTML中打印一些变量

<div class="row">
    <div class="col-sm-4 col-xs-12 pull-right">
         <img src="../images/showRooms/<?php print $roomImage ?>" width="256"
         height="256" alt="" class="img-responsive img-thumbnail"/>
   </div>
         <div class="col-sm-8 col-xs-12 pull-right">
              <div class="pull-right">
                   <h4 class="text-bold">نبذه عن القاعة</h4>
                       <?php print $roomDetails ?>
              </div>
        </div>
</div>

现在来了 while 循环

while (mysqli_stmt_fetch($getShowroomDetails)) {
   printf("%s %s'n", $activityName, $activityPlace);
}

这里while loop只读第一行?

由于您需要打印所有记录,因此您可以使用mysqli_fetch_array:

mysqli_fetch_array($result,MYSQLI_ASSOC); // Associative array

在代码中执行以下更改:

 while ($row = mysqli_fetch_array($getShowroomDetails,MYSQLI_ASSOC)) {
       print_r($row); // it gives all the records
      // printf("%s %s'n", $activityName, $activityPlace); // I don't know why you have used it, that's why I've commented this line
    }
如果我

答对了问题,你正在寻找mysqli_fetch_all((

有关更多详细信息,请参阅 http://php.net/manual/en/mysqli-result.fetch-all.php。

注意你的记忆。如果您假设结果很大,更好(对内存的影响较小(的方法是在模板的循环中处理它。在您的情况下,这可能会略有不同,具体取决于您的设置:)

<div>
    <h4 class="text-bold">نبذه عن القاعة</h4>
    <?php 
        while (mysqli_stmt_fetch($getShowroomDetails)) {
            printf("%s %s'n", $activityName, $activityPlace);
        }
        print $roomDetails ?>
</div>
相关文章: