forech 显示比数据库少一行
forech shows one row less than the database
我有一个 if 语句,如果我的 table 中存在某种条件,它会动态显示 html 数据库中存在的每一行的数据,我正在使用foreach 循环,但它一直显示在比实际 table 少的行上,如果它有三行,它会显示两行,如果只有一行它什么都不显示,它会通过 if 语句,因为如果没有显示错误消息,但在我的情况下它只显示空白页,为什么会这样?
function show()
{
$stmt = $this->db->prepare("SELECT offered_rides.*, reg.*
FROM offered_rides
JOIN reg ON offered_rides.phone = reg.phone where
offered_rides.date = :date AND offered_rides.seats >= :seats AND offered_rides.start = :start AND offered_rides.finish = :finish ");
$stmt->bindParam(':date',$this->date);
$stmt->bindParam(':seats',$this->seats);
$stmt->bindParam(':start',$this->from);
$stmt->bindParam(':finish',$this->to);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row)
{
foreach($stmt as $row):
?>
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"> <?php echo $row["fname"];?> </p>
<p style="display:inline;"> <?php echo $row["lname"];?> </p>
</div>
</div>
//i need to store these to use them later
<input type = "hidden" name = "phone-D" value = "<?=$row['phone'] ?>" />
<input type = "hidden" name = "time" value = "<?= $row['time'] ?>" />
<input type = "hidden" name = "seats" value = "<?= $row['seats'] - $this->seats?>" />
</form>
<?php endforeach;
}
这完全没有意义:foreach($stmt as $row):
。 $stmt
不是数组,它是单个对象。相反,您应该使用 while
循环,并以 $row = $stmt->fetch(PDO::FETCH_ASSOC);
步骤作为条件。因为 fetch()
方法 returns 每次调用时都是一行。这就是你想要的。
function show() {
$stmt = $this->db->prepare("SELECT offered_rides.*, reg.* FROM offered_rides JOIN reg ON offered_rides.phone = reg.phone WHERE offered_rides.date = :date AND offered_rides.seats >= :seats AND offered_rides.start = :start AND offered_rides.finish = :finish ");
$stmt->bindParam(':date',$this->date);
$stmt->bindParam(':seats',$this->seats);
$stmt->bindParam(':start',$this->from);
$stmt->bindParam(':finish',$this->to);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"> <?php echo $row["fname"];?> </p>
<p style="display:inline;"> <?php echo $row["lname"];?> </p>
</div>
</div>
//i need to store these to use them later
<input type = "hidden" name = "phone-D" value = "<?=$row['phone'] ?>" />
<input type = "hidden" name = "time" value = "<?= $row['time'] ?>" />
<input type = "hidden" name = "seats" value = "<?= $row['seats'] - $this->seats?>" />
</form>
<?
}
}
提示:当在 php 中使用“短标签”时,您的 html 标记会更容易阅读:
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"><?= $row["fname"]?></p>
<p style="display:inline;"><?= $row["lname"]?></p>
</div>
</div>
//i need to store these to use them later
<input type="hidden" name="phone-D" value="<?=$row['phone']?>"/>
<input type="hidden" name="time" value="<?= $row['time']?>"/>
<input type="hidden" name="seats" value="<?= $row['seats'] - $this->seats?>"/>
</form>
我有一个 if 语句,如果我的 table 中存在某种条件,它会动态显示 html 数据库中存在的每一行的数据,我正在使用foreach 循环,但它一直显示在比实际 table 少的行上,如果它有三行,它会显示两行,如果只有一行它什么都不显示,它会通过 if 语句,因为如果没有显示错误消息,但在我的情况下它只显示空白页,为什么会这样?
function show()
{
$stmt = $this->db->prepare("SELECT offered_rides.*, reg.*
FROM offered_rides
JOIN reg ON offered_rides.phone = reg.phone where
offered_rides.date = :date AND offered_rides.seats >= :seats AND offered_rides.start = :start AND offered_rides.finish = :finish ");
$stmt->bindParam(':date',$this->date);
$stmt->bindParam(':seats',$this->seats);
$stmt->bindParam(':start',$this->from);
$stmt->bindParam(':finish',$this->to);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row)
{
foreach($stmt as $row):
?>
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"> <?php echo $row["fname"];?> </p>
<p style="display:inline;"> <?php echo $row["lname"];?> </p>
</div>
</div>
//i need to store these to use them later
<input type = "hidden" name = "phone-D" value = "<?=$row['phone'] ?>" />
<input type = "hidden" name = "time" value = "<?= $row['time'] ?>" />
<input type = "hidden" name = "seats" value = "<?= $row['seats'] - $this->seats?>" />
</form>
<?php endforeach;
}
这完全没有意义:foreach($stmt as $row):
。 $stmt
不是数组,它是单个对象。相反,您应该使用 while
循环,并以 $row = $stmt->fetch(PDO::FETCH_ASSOC);
步骤作为条件。因为 fetch()
方法 returns 每次调用时都是一行。这就是你想要的。
function show() {
$stmt = $this->db->prepare("SELECT offered_rides.*, reg.* FROM offered_rides JOIN reg ON offered_rides.phone = reg.phone WHERE offered_rides.date = :date AND offered_rides.seats >= :seats AND offered_rides.start = :start AND offered_rides.finish = :finish ");
$stmt->bindParam(':date',$this->date);
$stmt->bindParam(':seats',$this->seats);
$stmt->bindParam(':start',$this->from);
$stmt->bindParam(':finish',$this->to);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"> <?php echo $row["fname"];?> </p>
<p style="display:inline;"> <?php echo $row["lname"];?> </p>
</div>
</div>
//i need to store these to use them later
<input type = "hidden" name = "phone-D" value = "<?=$row['phone'] ?>" />
<input type = "hidden" name = "time" value = "<?= $row['time'] ?>" />
<input type = "hidden" name = "seats" value = "<?= $row['seats'] - $this->seats?>" />
</form>
<?
}
}
提示:当在 php 中使用“短标签”时,您的 html 标记会更容易阅读:
<form action="Update.php" method="POST">
<div class="a">
<div class="fnamelname">
<p style="display:inline;"><?= $row["fname"]?></p>
<p style="display:inline;"><?= $row["lname"]?></p>
</div>
</div>
//i need to store these to use them later
<input type="hidden" name="phone-D" value="<?=$row['phone']?>"/>
<input type="hidden" name="time" value="<?= $row['time']?>"/>
<input type="hidden" name="seats" value="<?= $row['seats'] - $this->seats?>"/>
</form>