我试图将MySQL中的数据插入到表中但是我似乎无法使循环正常工作:(
select语句是正确的,我不能让它正确地回显数据。
有什么想法吗?
编辑的代码低于我修正的:)
错误是
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>
答案 0 :(得分:1)
这一行没有任何意义:
while($row = $data->(PDO::FETCH_ASSOC))
我认为您需要的是based on the docs)
foreach ($data as $row)
因为fetchAll
返回一个数组。
答案 1 :(得分:0)
它应该是,
$data = $query->fetchALL();
foreach($data as $row)
而不是
while($row = $data->(PDO::FETCH_ASSOC))
此外,
<?php } ?>
直到你没有配置短语
<? } ?>
答案 2 :(得分:0)
@jon是正确的,你需要一个foreach循环,你需要将PDO :: FETCH_ASSOC放入$ query-&gt; fetchAll(PDO :: FETCH_ASSOC)