gpt4 book ai didi

php - 我的 pdo 结果在表中重复

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:51 24 4
gpt4 key购买 nike

我有一个排行榜,它告诉我哪些人的金钱(用于测试目的的虚拟货币)处于领先地位。所以它有效,它显示了从我的表到 PDO(PHP 数据对象) 的所有内容。

问题是,我让它在表格中输出。但是对于每个 id,都会创建第二个这样的表:

enter image description here

我使用的代码:

<?php 
$db = new PDO('mysql:host=localhost;dbname=test1','root','####');

$sql = "SELECT naam, id, days, money FROM user ORDER BY money DESC ";
$results = $db->query($sql);



foreach($results as $row)
{

?>


<html>
<head>



</head>
<body>
<TABLE BORDER="5" WIDTH="100%" CELLPADDING="4" CELLSPACING="3">
<TR>
<TH><BR><H3>POINTS LEADERBOARD</H3>
</TH>
</TR>
<TR>
<TH>Username</TH>
<TH>Money</TH>
<TH>Days</TH>
</TR>
<TR>
<TD><?php echo $row['naam']; ?></TD>
<TD><?php echo $row['money']?></TD>
<TD><?php echo $row['days']?></TD>
</TR>
</TABLE>

<?php
/*
echo "<table>";
echo "<TR>";
echo "<TH><BR><H3>POINTS LEADERBOARD</H3>";
echo "</TH>";
echo "</TR>";

echo "<TR>";
echo "<TH>Username</TH>";
echo "<TH>Money</TH>";
echo "<TH>Days</TH>";
echo "</TR>";

echo "<TR>";
echo "<TD>"; echo $row['naam']; echo "</TD>";
echo "<TD>"; echo $row['money']; echo "</TD>";
echo "<TD>"; echo $row['days']; echo "</TD>";
echo "</TR>";
echo "</table>";
*/
?>




<?php } ?>
</body>
</html>

任何人都可以帮助我或告诉我我做错了什么吗?

谢谢!

最佳答案

您在循环中使用开始 html 等标签的方式是错误的 - 也许是这样的?

<?php 
$db = new PDO('mysql:host=localhost;dbname=test1','root','####');
$sql = 'SELECT naam, id, days, money FROM user ORDER BY money DESC ';
$results = $db->query($sql);
?>
<html>
<head>
<title>leaderboard</title>
</head>
<body>
<?php
echo "
<TABLE BORDER='5' WIDTH='100%' CELLPADDING='4' CELLSPACING='3'>
<TR>
<TH><BR><H3>POINTS LEADERBOARD</H3></TH>
</TR>
<TR>
<TH>Username</TH>
<TH>Money</TH>
<TH>Days</TH>
</TR>";

foreach($results as $row) {
echo "
<TR>
<TD>{$row['naam']}</TD>
<TD>{$row['money']}</TD>
<TD>{$row['days']}</TD>
</TR>";
}
echo "</TABLE>";
?>
</body>
</html>

关于php - 我的 pdo 结果在表中重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34323930/

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