2014-01-13 64 views
0

我嘗試從MySQL數據插入表,但是我似乎無法得到循環工作correcty :(PHP PDO填寫表格與陣列從數據庫

查詢語句是正確的,我只是不能得到它了正確呼應數據。

任何想法?

編輯的代碼下面是我固定的:)

有誤差

Parse error: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/4507408/public_html/review.php on line 67 

<?php 

$sql = "SELECT u.userID, p.productID, p.name, c.categoryname, r.reviewID, r.reviewTime, r.reviewData, u.firstname FROM review r INNER JOIN product p on p.productID = r.productID INNER JOIN user u on u.userID = r.userID INNER JOIN category c on c.categoryID = p.categoryID ORDER BY $sortby ASC;"; 

$query = $DBH->prepare($sql); 
$query->execute(); 
$data = $query->fetchALL(); 
$originalDate = $row['reviewTime']; 
$newDate = date("d-m-Y", strtotime($originalDate)); 
while($row = $data->(PDO::FETCH_ASSOC))      
{ 

?> 

<table class="rev"> 

<tr> 
<th><a href="review.php?sort=name">Product Name:</a></th> 
<th><a href="review.php?sort=categoryname">Category</a></th> 
<th><a href="review.php?sort=reviewID">ID</a></th> 
<th><a href="review.php?sort=reviewTime">Date</a></th> 
<th><a href="review.php?sort=reviewData">Review</a></th> 
<th><a href="review.php?sort=firstname">Reviewer's Name</a></th> 
</tr> 


<td><a href="viewproduct.php?productID=<?=$row['productID']?>"><?=$name?></a></td> 
<td><?=$row['categoryname']?></td> 
<td><?=$row['reviewID']?></td> 
<td><?=$newDate?></td> 
<td><?=$row['reviewData']?></td> 
<td><?=$row['firstname']?></td> 
</tr> 

<?  } ?> 

正確的代碼:

           <?php 

               $sql = "SELECT u.userID, p.productID, p.name, c.categoryname, r.reviewID, r.reviewTime, r.reviewData, u.firstname FROM review r INNER JOIN product p on p.productID = r.productID INNER JOIN user u on u.userID = r.userID INNER JOIN category c on c.categoryID = p.categoryID ORDER BY $sortby ASC;"; 

               $query = $DBH->prepare($sql); 
               $query->execute(); 
               $data = $query->fetchALL(); 

               ?> 

            <table class="rev"> 

               <tr> 
               <th><a href="review.php?sort=name">Product Name:</a></th> 
               <th><a href="review.php?sort=categoryname">Category</a></th> 
               <th><a href="review.php?sort=reviewID">ID</a></th> 
               <th><a href="review.php?sort=reviewTime">Date</a></th> 
               <th><a href="review.php?sort=reviewData">Review</a></th> 
               <th><a href="review.php?sort=firstname">Reviewer's Name</a></th> 
               </tr> 

               <? foreach ($data as $row): ?> 
               <? 
               $originalDate = $row['reviewTime']; 
               $newDate = date("d-m-Y", strtotime($originalDate)); 
               ?> 
               <tr> 
                <td><a href="viewproduct.php?productID=<?=$row['productID']?>"><?=$row['productID']?></a></td> 
                <td><?=$row['categoryname']?></a></td> 
                <td><?=$row['reviewID']?></a></td> 
                <td><?=$newDate?></a></td> 
                <td><?=$row['reviewData']?></a></td> 
                <td><?=$row['firstname']?></a></td> 
               </tr> 


               <? endforeach?> 


             </table> 

回答

1

此行沒有任何意義:

while($row = $data->(PDO::FETCH_ASSOC)) 

我想你需要什麼(based on the docs)是

foreach ($data as $row) 

fetchAll返回一個陣列。

0

它應該是,

$data = $query->fetchALL(); 
foreach($data as $row)  

,而不是

while($row = $data->(PDO::FETCH_ASSOC)) 

此外,

<?php } ?> 

直到你沒有配置shortag

<? } ?> 
+0

謝謝我已經解決了它並使用正確的代碼更新了問題。 – Adam91Holt

+0

在轉移到PDO的過程中 – Adam91Holt

0

@jon是正確的,您需要一個foreach循環,並且您需要將PDO :: FETCH_ASSOC放入$ query-> fetchAll(PDO :: FETCH_ASSOC)