0

我有一个简单的 JOIN 查询:

$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp 
                            INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon 
                            INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
                            INNER JOIN tblclients cl ON cl.id = ho.userid');

while ($data = $lstCheck->fetch())
{
            $serveridx = $data['ho.id'];
            $clientid = $data['cl.id'];
}

我的问题是我在 tblhosting 和 tblclients 表中都有一个“id”列,所以我的变量都有相同的 id。我尝试在上面的示例中使用别名(ho. 和 cl.)设置它,但它不起作用。如何在上面的示例中执行此操作?

谢谢!

4

2 回答 2

2

这个怎么样?(对 php 细节有点生疏,但应该这样做):


$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp 
                            INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon 
                            INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
                            INNER JOIN tblclients cl ON cl.id = ho.userid');

while ($data = $lstCheck->fetch())
{
            $serveridx = $data['hid'];
            $clientid = $data['cid'];
}
于 2012-05-11T03:41:03.693 回答
1

您正在选择带有通配符的记录,*因此您不能选择这样的字段。

根据您的查询h.hst_serveridho.userid具有与您想要的完全相同的值。所以只需这样做

while ($data = $lstCheck->fetch())
{
            $serveridx = $data['hst_serverid'];
            $clientid = $data['userid'];
}

但是,选择特定的行可能听起来也更好

$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');

while ($data = $lstCheck->fetch())
{
            $serveridx = $data['hid'];
            $clientid = $data['cid'];
}
于 2012-05-11T03:44:43.660 回答