dsgffz2579 2015-07-13 13:14
浏览 47
已采纳

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

    }   
} 

展开全部

  • 写回答

1条回答 默认 最新

  • doujiu6976 2015-07-13 13:19
    关注

    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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部