2013-08-07 120 views
-2

我已經嘗試了幾個不同版本的PDO while while循環,並沒有一個返回多個行。問題在於呼叫循環中的dreamyPrizes表。如果我把它拿出來,它可以正常工作,它只會返回一行。PDO while循環只返回一行

我想:

$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

1.第一個查詢中擺脫無用的參數進行輸出。使用JOIN將它全部用** 1 **查詢。 3. **總是**在你的腳本中有'error_reporting(E_ALL)' –

回答

-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` 
+0

仍然$ prizeId從哪裏冒出來 –

+0

WOW我的代表在這篇文章中遭到了毆打。你們知道我花了多長時間才能完成這項工作? – user2320607

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 ?>