Php 从mySQL数据库读取多个表?

Php 从mySQL数据库读取多个表?,php,mysql,Php,Mysql,好吧,我不能在标题中具体说明我的问题,所以请原谅我 我制作了一个.php脚本,从数据库中读取数据并将其打印到页面上的一个表中。基本上,它应该充当banlist。但我有一个问题。由于ban在数据库中的几个不同表中排序,所以我需要阅读所有ban,以了解我在banlist上需要的具体细节。我已经完成了大部分工作,但是我不能得到禁止“骗子”的管理员的名字。下面是“管理员id”如何位于“惩罚”表中,管理员的名称如何位于“客户”表中。现在我不知道如何通过“惩罚”表中的“admin_id”从“clients”

好吧,我不能在标题中具体说明我的问题,所以请原谅我

我制作了一个.php脚本,从数据库中读取数据并将其打印到页面上的一个表中。基本上,它应该充当banlist。但我有一个问题。由于ban在数据库中的几个不同表中排序,所以我需要阅读所有ban,以了解我在banlist上需要的具体细节。我已经完成了大部分工作,但是我不能得到禁止“骗子”的管理员的名字。下面是“管理员id”如何位于“惩罚”表中,管理员的名称如何位于“客户”表中。现在我不知道如何通过“惩罚”表中的“admin_id”从“clients”表中获取管理员的名称,并将其打印在同一页上

这就是我所做的,我只是错过了“管理员”的名字

下面是从数据库中读取当前信息的代码

mysql_query("SELECT penalties.id, penalties.type, penalties.time_add, penalties.time_expire,
                    penalties.reason, penalties.inactive, penalties.duration, penalties.admin_id,
                    target.id as target_id, target.name as target_name, target.ip as target_ip 
             FROM penalties, clients as target 
             WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')
                  AND inactive = 0 
                  AND penalties.client_id = target.id
             ORDER BY penalties.id DESC") 
  or die(mysql_error());

这将为您指明正确的方向:

SELECT 
penalties.id, penalties.type, penalties.time_add, penalties.time_expire, penalties.reason, penalties.inactive, penalties.duration, penalties.admin_id, 
clients.id as target_id, clients.name as target_name, clients.ip as target_ip 
FROM penalties 
LEFT JOIN clients
ON penalties.client_id = clients.id 
WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')  AND inactive = 0
ORDER BY penalties.id DESC

您有两个从惩罚返回到客户端的逻辑连接。因此,你需要两个连接回来。一个用于“目标”一个用于“管理员”


您需要使用一个连接。您想了解。建议使用mysqli或PDO_MySQL扩展。不建议在新开发中使用旧的mysql扩展。这不是只返回骗子的名字吗?
SELECT penalties.id, penalties.type, penalties.time_add, penalties.time_expire, penalties.reason, 
       penalties.inactive, penalties.duration, penalties.admin_id, target.id as target_id, 
       target.name as target_name, target.ip as target_ip,
       admin.name
FROM penalties, clients as target, clients as admin

WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')  
  AND inactive = 0 
  AND penalties.client_id = target.id 
  AND penalties.admin_ID = admin.id

ORDER BY penalties.id DESC