【问题标题】:while loop under fetch() prints the first row onlyfetch() 下的 while 循环仅打印第一行
【发布时间】:2015-12-10 08:17:51
【问题描述】:
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 只读取第一行?

【问题讨论】:

  • 没有while循环给你所有可能的查询检查结果php.net/manual/en/mysqli-stmt.bind-result.php
  • Grate,为什么它只在循环下获取一行?
  • 向我们展示您如何使用 whlie 循环
  • 就像它在我的问题中发布的一样,看看我是否删除了上面的$getShowroomDetails-&gt;fetch(); while loop 工作正常,但当然其他三个变量不会显示$roomName$roomDetails,。 ..
  • 如果你想获取多于一行,'mysqli_fetch_assoc' 不工作吗?

标签: php mysqli while-loop


【解决方案1】:

由于您需要打印所有记录,您可以使用 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_array() 期望参数 1 为 mysqli_result,给定对象
  • 它是因为您的查询应该分配给一个变量,例如:$query='your query'; $getShowroomDetails=mysqli_query($con,$query); 其中 $con 是连接对象
【解决方案2】:

如果我没看错,你正在寻找 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>

【讨论】:

  • 感谢您的回复。但我不明白你在回答中说了什么?请给出一个清楚的例子你刚刚写的我做了但仍然没有工作
  • 不知何故我想念你的问题,对不起。 - 如果你只想要第一个元素,你可以这样做:$roomDetail = mysqli_stmt_fetch($getShowroomDetails);没有时间
猜你喜欢
  • 2017-11-09
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2016-08-06
相关资源
最近更新 更多