gpt4 book ai didi

php pdo用数据库中的数组填充表格

转载 作者:行者123 更新时间:2023-11-29 13:19:04 25 4
gpt4 key购买 nike

我正在尝试将数据从 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>

最佳答案

这一行没有任何意义:

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

我认为您需要的是( based on the docs )

foreach ($data as $row)

因为 fetchAll 返回一个数组。

关于php pdo用数据库中的数组填充表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21096805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com