问题从PHP中查询SQL查询的结果

时间:2014-07-22 15:50:47

标签: php mysql sql

我的代码执行SQL查询,如下所示:

$query = 'SELECT 
        cl.*,c.id,c.type,c.firstname,c.surname,c.job,c.company,c.directorycompany_id, dc.id, dc.name, es.id, GROUP_CONCAT(dirc.name), GROUP_CONCAT(dirp.name), GROUP_CONCAT(dirs.name) 
        FROM contactlist_contact cl '.
        'INNER JOIN contact c ON cl.contact_id = c.id '.
        'LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id '.
        'LEFT JOIN expertsection es ON es.id = c.expertsection_id '.
        'LEFT JOIN directorycolumn dirc ON dirc.directorycontact_id = c.id '.
        'LEFT JOIN directoryprogramme dirp ON dirp.directorycontact_id = c.id '.
        'LEFT JOIN directorysupplement dirs ON dirs.directorycontact_id = c.id '.
        'WHERE cl.contactlist_id = '.$contactListId.' ORDER BY dc.surname';

$db->query($query,$t);

我尝试检索返回结果的内容,如下所示:

while($db->next($t))
{
    $contact = new DirectoryContact(0);
    $contact->setId($db->get('c.id',$t));
    $contact->setContactType($db->get('c.type',$t));
    $contact->setFirstname($db->get('c.firstname',$t));
    $contact->setSurname($db->get('c.surname',$t));
    $contact->setJob($db->get('c.job',$t));
    $contact->setCompany($db->get('c.company',$t));
    $contact->setDisplayColumn($db->get('GROUP_CONCAT(dirc.name)', $t));
    $contact->setDisplaySupplement($db->get('GROUP_CONCAT(dirs.name)', $t));
    $contact->setDisplayProgramme($db->get('dirp.name', $t));

    $directoryCompany = new DirectoryCompany(0);
    $directoryCompany->setId($db->get('dc.id',$t));
    $directoryCompany->setName($db->get('dc.name',$t));

    $expertSection = new ExpertSection(0);
    $expertSection->setId($db->get('es.id',$t));

    $contact->setDirectoryCompany($directoryCompany);
    $contact->setExpertSection($expertSection);

    $this->contacts->add($contact);
}

出于某种原因,每次这些行都会返回false

$contact->setDisplayColumn($db->get('GROUP_CONCAT(dirc.name)', $t));
$contact->setDisplaySupplement($db->get('GROUP_CONCAT(dirs.name)', $t));
$contact->setDisplayProgramme($db->get('GROUP_CONCAT(dirp.name)', $t));

有人知道我哪里错了吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

我不知道它会解决这个问题,但也许你可以尝试使用别名; e.g:

$query = 'SELECT cl.*,c.id,c.type,c.firstname,c.surname,c.job,c.company,c.directorycompany_id, dc.id, dc.name, es.id, GROUP_CONCAT(dirc.name) AS gcname, GROUP_CONCAT(dirp.name) AS gpname, GROUP_CONCAT(dirs.name) AS gsname 
FROM contactlist_contact cl '.
'INNER JOIN contact c ON cl.contact_id = c.id '.
'LEFT JOIN directorycompany dc ON dc.id = c.directorycompany_id '.
'LEFT JOIN expertsection es ON es.id = c.expertsection_id '.
'LEFT JOIN directorycolumn dirc ON dirc.directorycontact_id = c.id '.
'LEFT JOIN directoryprogramme dirp ON dirp.directorycontact_id = c.id '.
'LEFT JOIN directorysupplement dirs ON dirs.directorycontact_id = c.id '.
'WHERE cl.contactlist_id = '.$contactListId.' ORDER BY dc.surname';
$db->query($query,$t);

然后,而不是:

$contact->setDisplayColumn($db->get('GROUP_CONCAT(dirc.name)', $t));
$contact->setDisplaySupplement($db->get('GROUP_CONCAT(dirs.name)', $t));
$contact->setDisplayProgramme($db->get('GROUP_CONCAT(dirp.name)', $t));

执行:

$contact->setDisplayColumn($db->get('gcname', $t));
$contact->setDisplaySupplement($db->get('gsname', $t));
$contact->setDisplayProgramme($db->get('gpname', $t));