使用PDO输出多个用户禁止数据

时间:2015-07-13 21:14:54

标签: php pdo

我正在尝试显示当前的用户禁令。它们会显示,但当我尝试为当前用户提供fetchAll数据时,它只会输出1个详细信息。

这是我的代码

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>";

    }   
} 

1 个答案:

答案 0 :(得分:0)

您必须在打印表格每一行的代码周围放置foreach循环。

$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>";
}