PHP使用JOINS从MySQL显示数据

时间:2018-08-19 19:49:58

标签: php html mysql mysqli

我正在使用PHP 5.6,但我却遇到了php错误,并且一切都没有。

我的PHP代码

$GetAllServers  = $db->select("SELECT * FROM panel_servers ORDER BY id ASC");
$GetAllGroups   = $db->select("SELECT * FROM panel_admin_groups ORDER BY id");
$GetAllAdmins   = $db->select("SELECT *, s.name server_name, g.name group_name, a.pid pid
    FROM panel_admins a 
    LEFT JOIN panel_players p ON a.pid = p.id 
    LEFT JOIN panel_admin_groups g ON a.gid = g.id 
    LEFT JOIN panel_countries c ON p.country = c.country_code 
    LEFT JOIN (SELECT `username`, `pid` FROM panel_players_username GROUP BY pid ORDER BY last_used DESC) u ON a.pid = u.pid 
        LEFT JOIN panel_servers s ON a.sid = s.id WHERE (TIMESTAMPDIFF(MINUTE, add_time, now()) < g.usetime OR g.usetime = 0) ORDER BY immunity DESC");

我的HTML代码

<?php
$Players = array();
foreach ((array)$GetAllAdmins as $admin)
    if(!in_array($admin['steamid'], $Players))
        array_push($Players, $admin['steamid']);
        $avatars = GetPlayersAvatars($Players);
?>
<?php foreach ((array) $GetAllAdmins as $admin) { 
    if(!empty($admin['steamid'])){?>
        <tr>
            <td class="coutryrow"><img src="<?=WEBSITE;?>/img/flag/<?=$admin['country_name'];?>.png" title="<?=$admin['country_name'];?>" class="flag"></td>
            <td>
                <a target="_blank" href="http://steamcommunity.com/profiles/<?=$admin['steamid'];?>">
                    <img src="<?=$avatars[$admin['steamid']]?>" class="player-avatar">
                    <?=(!empty($admin['player_username'])) ? htmlspecialchars($admin['player_username']) : htmlspecialchars($avatars['username-'.$admin['steamid']]);?>
                </a>
             </td>
             <td class="centerrow"><?=htmlspecialchars($admin['group_name']);?></td>
             <td class="centerrow"><?=htmlspecialchars($admin['server_name']);?></td>
             <td class="centerrow"><?=date(TIMEFORMAT, strtotime($admin['add_time']));?></td>
             <td class="centerrow"><a href="<?=$CurrentURL;?>edit/<?=intval($admin['aid']);?>/" class="btn btn-info btn-mini"><span class="icon"> <i class="icooo-on-edit"></i> </span><?= _("Edit");?></a></td>
         </tr>

         <?php } else {echo "<tr><td>-</td></tr>";} } ?>

我想在HTML中显示添加到数据库中的所有管理员的列表,但是例如名称在一个表中,而标志在另一个表中。有了JOINS和我想显示在html上的所有内容,

1 个答案:

答案 0 :(得分:0)

如果您希望数据库中最后添加的管理员使用此查询

$GetAllAdmins   = $db->select("SELECT steamid, s.name server_name, g.name group_name, a.pid pid FROM panel_admins a 
                                        LEFT JOIN panel_admin_groups g ON a.gid = g.id 
                                        LEFT JOIN panel_servers s ON a.sid = s.id WHERE (TIMESTAMPDIFF(MINUTE, add_time, now()) < g.usetime OR g.usetime = 0) ORDER BY immunity DESC");

您需要3列。如果要添加过滤器,请在WHERE子句中添加