使用PDO输出多个用户禁止数据
问题描述:
I am trying to display the current users bans. They do display, but it only outputs 1 detail, when I'm trying to fetchAll
data for that current user.
Here is my code
function getPlayerBans($username, $conn) {
$getPlayerBans = $conn->prepare("SELECT id, player, bannedby, comment, expires, time FROM `bans` WHERE player = :Player");
$getPlayerBans->bindValue(":Player", $username);
$getPlayerBans->execute();
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
foreach($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
}
if($getPlayerBans->rowCount() == 0)
{
echo('No Results Available');
}else{
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>
<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>
</tbody>
</table>";
}
}
答
You have to put the foreach
loop around the code that prints each row of the table.
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
if (count($rows) == 0) {
echo('No Results Available');
} else {
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>";
foreach ($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
echo "<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>";
}
echo "</tbody>
</table>";
}