我已经尝试了几个不同版本的PDO while循环,但没有一个版本返回多行。问题是在循环中调用dreamyPrizes表。如果我把它拿出来就可以了,用它,它只返回一行。
我试过了:
$st = $db->prepare("SELECT * FROM `dreamyAuctions` WHERE `showOnHomePage` =:yes AND `active`=:yes AND `completed`=:no ORDER BY `StartDate`"); // need to filter for next auction
$st->bindParam(':yes', $yes); // filter
$st->bindParam(':no', $no); // filter
$st->bindParam(':gameId', $gameId); // filter
$yes=1;
$no=0;
$st->execute();
$r = $st->fetch(PDO::FETCH_ASSOC);
<?
while($r = $st->fetch(PDO::FETCH_ASSOC)){?>
<?
$auctionId= $r['id'];
$startDate = date("m-d-Y h:i A", strtotime($startDate));
if (strlen($title) > 100)
$title = substr($title, 0, 90) . '...</a>';
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$st = $db->prepare("SELECT * FROM `dreamyPrizes` WHERE `id`=:prizeId"); // need to filter for next auction
$st->bindParam(':prizeId', $prizeId); // filter
$st->execute();
$r = $st->fetch(PDO::FETCH_ASSOC);
$prizeImage= $r['image1'];
$retailPrice= $r['retailPrice'];
?>
<div>div to loop</div> <? }?>
我也试过
do {
$auctionId= $r['id'];
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$st = $db->prepare("SELECT * FROM `dreamyPrizes` WHERE `id`=:prizeId"); // need to filter for next auction
$st->bindParam(':prizeId', $prizeId); // filter
$st->execute();
$rs = $st->fetch(PDO::FETCH_ASSOC);
$prizeImage= $rs['image1'];
$retailPrice= $rs['retailPrice'];
?>
<div>div to loop</div>
<?php } while ($r = $st->fetch(PDO::FETCH_ASSOC)); ?>
答案 0 :(得分:0)
$sql = "SELECT p.* FROM `dreamyAuctions` a JOIN `dreamyPrizes` p
ON p.id = a.id
WHERE `showOnHomePage` = 1 AND `active`= 0 AND `completed`= 0
ORDER BY `StartDate`";
$stm = $db->query($sql);
$data = $stm->fetchAll();
这就是获取数据所需的全部内容 现在,您可以从分离的模板
继续输出<? foreach ($data as $row): ?>
<div>div to loop <?=$row['image1']?> <?=$row['retailPrice']?></div>
<? endforeach ?>
答案 1 :(得分:-1)
必须将第二个表中的“st”更改为“sts”才能使其正常工作
$sts = $db->prepare("SELECT * FROM `dreamyPrizes` WHERE `id`=:prizeId"); // need to filter for next auction
$sts->bindParam(':prizeId', $prizeId); // filter
$sts->execute();
$rs = $sts->fetch(PDO::FETCH_ASSOC);
$prizeImage= $rs['image1'];
$retailPrice= $rs['retailPrice'];`enter code here`