0

我的代码中出现了一个问题,但是当我在 phpmyadmin 中测试查询时,我得到了NULL(就像它应该是的那样)

我想加入几个表来显示有关用户的所有信息。用户表包含有关客户端的基本信息。在我的页面上,我想添加有关用户可能的医生访问、物理治疗师访问和专家治疗的信息。每个连接都存储在不同的表中,如下所示:


+------+-------------+--------------+
| n_id | n_client_id | n_connect_id |
+------+-------------+--------------+
|  1   |     1       |       1      |
+------+-------------+--------------+
|  2   |     1       |       2      |
+------+-------------+--------------+

n_connect_id可以是存储在匹配表中的医生物理治疗师和/或专家记录的 ID。

因为并非所有用户都会在所有三个(医生、物理治疗师和专家)表中都有记录,所以在某些情况下NULL会是输出。

当我在 phpmyadmin 中测试我的查询时,$client = 1;我得到以下输出(这是正确的,因为医生的访问仅适用于client_id 1):


+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+
| clientId | clientName | clientAddres  | clientZip | clientCity | clientPhone | clientMail   | clientDoctorCompany | clientDoctor | clientPhysioCompany | clientPhysio | clientSpecialist | clientSpecialistsSpecialities | clientDoctorRecords | clientDoctorRecordsDate | clientDoctorRecordsParaph |
+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+
| 1        | Name       | Address 1     | 1234 AB   | City       | 0612345678  | info@url.com | Company 1           | Doctor 1     | Physio Company  1   | Therapist 1  | Specialist 1     | specialty 1, specialty 2      | visit 1             | 2012-11-11              | MK                        |
+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+

当我在 phpmyadmin 中测试我的查询时,$client = 2;我得到以下输出(这是正确的,因为医生的访问仅适用于client_id 1):


+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+
| clientId | clientName | clientAddres  | clientZip | clientCity | clientPhone | clientMail   | clientDoctorCompany | clientDoctor | clientPhysioCompany | clientPhysio | clientSpecialist | clientSpecialistsSpecialities | clientDoctorRecords | clientDoctorRecordsDate | clientDoctorRecordsParaph |
+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+
| 1        | Name       | Address 1     | 1234 AB   | City       | 0612345678  | info@url.com | Company 1           | Doctor 1     | Physio Company  1   | Therapist 1  | Specialist 1     | specialty 1, specialty 2      | NULL                | NULL                    | NULL                      |
+----------+------------+---------------+-----------+------------+-------------+--------------+---------------------+--------------+---------------------+--------------+------------------+-------------------------------+---------------------+-------------------------+---------------------------+

当我在我的网站上运行代码时,它会不断输出所有记录,如果我更改$client

这是我使用的代码


<?php

    class ClientData{
        public $clientId;
        public $clientName;
        public $clientAddress;
        public $clientZip;
        public $clientCity;
        public $clientPhone;
        public $clientMail;
        public $clientDoctor;
        public $clientDoctorCompany;
        public $clientDoctorRecords;
        public $clientDoctorRecordsDate;
        public $clientDoctorRecordsParaph;
        public $clientPhysio;
        public $clientPhysioCompany;
        public $clientSpecialist;
        public $clientSpecialistsSpecialities;
    }

    class clientManagement{

        public $cInfo = Array();
        public $clientInfo = Array();
        public $querystring;

        [..]

        public function getClientDetails($client){

            $dbdata = new mySQLAccessData();
            $db = new PDO($dbdata->hostname,$dbdata->username,$dbdata->password);
            $sql = "
                SELECT 
                    client.c_id AS clientId, client.c_name AS clientName, client.c_address AS clientAddress, client.c_zip AS clientZip, client.c_city AS clientCity, client.c_tel AS clientPhone, client.c_mail AS clientMail, 
                    doctorCompany.d_name AS clientDoctorCompany, 
                    doctor.d_name AS clientDoctor, 
                    physioCompany.p_name AS clientPhysioCompany, 
                    physio.p_name AS clientPhysio, 
                    specialist.s_name AS clientSpecialist, 
                    GROUP_CONCAT(DISTINCT specialities.s_specialty) AS clientSpecialistsSpecialities, 
                    GROUP_CONCAT(DISTINCT dvr.dv_records) AS clientDoctorRecords, 
                    GROUP_CONCAT(DISTINCT dvr.dv_datetime) AS clientDoctorRecordsDate, 
                    GROUP_CONCAT(DISTINCT dvr.dv_paraph) AS clientDoctorRecordsParaph

                FROM adm_clients AS client

                    LEFT JOIN norm_client_doctor AS ncd ON ncd.ncd_client_id = client.c_id
                    LEFT JOIN adm_doctor_company AS doctorCompany ON doctorCompany.d_id = ncd.ncd_doctor_id
                    LEFT JOIN norm_doctor_company AS ndc ON ndc.ndc_company_id = doctorCompany.d_id
                    LEFT JOIN adm_doctor_person AS doctor ON doctor.d_id = ncd.ncd_doctor_id

                        LEFT JOIN adm_doctor_visit_records AS dvr ON dvr.dv_client_id = client.c_id

                    LEFT JOIN norm_client_physio AS ncp ON ncp.ncf_client_id = client.c_id
                    LEFT JOIN adm_physiotherapist_company AS physioCompany ON physioCompany.p_id = ncp.ncf_physio_id
                    LEFT JOIN norm_physio_company AS npc ON npc.nfc_company_id = physioCompany.p_id
                    LEFT JOIN adm_physiotherapist_person AS physio ON physio.p_id = npc.nfc_physio_id

                    LEFT JOIN norm_client_specialist AS ncs ON ncs.ncs_client_id = client.c_id
                    LEFT JOIN adm_specialist_person AS specialist ON specialist.s_id = ncs.ncs_specialist_id
                    LEFT JOIN norm_specialist_specialities AS nss ON nss.nsc_company_id = specialist.s_id
                    LEFT JOIN adm_specialist_specialities AS specialities ON specialities.s_id = nss.nsc_specialist_id

                WHERE client.c_id = '".$client."'
            ";
            $result = $db->query($sql); 
            $obj = $result->setFetchMode(PDO::FETCH_INTO, new ClientData);
            $i = 0;
            foreach($result as $show){
                $i++;
                $this->clientInfo[$i] = new ClientData();
                $this->clientInfo[$i]->clientId = $show->clientId;
                $this->clientInfo[$i]->clientName = $show->clientName;
                $this->clientInfo[$i]->clientAddress = $show->clientAddress;
                $this->clientInfo[$i]->clientZip = $show->clientZip;
                $this->clientInfo[$i]->clientCity = $show->clientCity;
                $this->clientInfo[$i]->clientPhone = $show->clientPhone;
                $this->clientInfo[$i]->clientMail = $show->clientMail;
                $this->clientInfo[$i]->clientDoctor = $show->clientDoctor;
                $this->clientInfo[$i]->clientDoctorCompany = $show->clientDoctorCompany;
                $this->clientInfo[$i]->clientDoctorRecords = $show->clientDoctorRecords;
                $this->clientInfo[$i]->clientDoctorRecordsDate = $show->clientDoctorRecordsDate;
                $this->clientInfo[$i]->clientDoctorRecordsParaph = $show->clientDoctorRecordsParaph;
                $this->clientInfo[$i]->clientPhysioCompany = $show->clientPhysioCompanyclientDoctorRecordsParaph;
                $this->clientInfo[$i]->clientPhysio = $show->clientPhysio;
                $this->clientInfo[$i]->clientSpecialist = $show->clientSpecialist;
                $this->clientInfo[$i]->clientSpecialistsSpecialities = $show->clientSpecialistsSpecialities;
            }
        }

        public function displayClientDetails(){
            $output = '<h3>Klantgegevens</h3>
                <div id="clientInfoContainer">
                <div id="clientStaticInfo">
                    <p><img src="'._BACKEND_URL.'/Inc/Im/icons/user_64.png" class="userImage"></p>
                    <p><small>Klant aangemaakt op 10/12/2012</small></p>
                    <p><small>Laatste wijziging op 10/12/2012</small></p>
                </div>
                <div id="clientDynamicInfo">
                    <table id="clientInfoTable">
                        <colgroup>
                            <col class="tableLabel">
                            <col class="tableValue">
                        </colgroup>
                        <thead>
                            <tr>
                                <th>Label</th>
                                <th>Waarde</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>Naam</td>
                                <td>'.$this->clientInfo[1]->clientName.'</td>
                            </tr>
                            <tr>
                                <td>Adres</td>
                                <td>'.$this->clientInfo[1]->clientAddress.'</td>
                            </tr>
                            <tr>
                                <td>Postcode woonplaats</td>
                                <td>'.$this->clientInfo[1]->clientZip.' '.$this->clientInfo[1]->clientCity.'</td>
                            </tr>
                            <tr>
                                <td>Telefoonnummer</td>
                                <td>'.$this->clientInfo[1]->clientPhone.'</td>
                            </tr>
                            <tr>
                                <td>Email</td>
                                <td>'.$this->clientInfo[1]->clientMail.'</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                </div>
                <div id="physio-visit-list" class="collapseContainer">
                    <h4>Praktijk fysiotherapie: <strong>'.$this->clientInfo[1]->clientPhysioCompany.'</strong>. Behandelend arts: <strong>'.$this->clientInfo[1]->clientPhysio.'</strong></h4>
                    <div class="collapsableContent">
                        <p>'.$this->clientInfo[1]->clientSpecialistsSpecialities.'</p>
                    </div>
                </div>
                <div id="doctor-visit-list" class="collapseContainer">
                    <h4>Dokterspraktijk: <strong>'.$this->clientInfo[1]->clientDoctorCompany.'</strong>. Behandelend arts: <strong>'.$this->clientInfo[1]->clientDoctor.'</strong></h4>
                    <div class="collapsableContent">
                        <p>'.$this->clientInfo[1]->clientDoctorRecords.'</p>
                    </div>
                </div>
            ';
            echo($output);
        }

    }
?>

我认为问题出在某处LEFT JOIN adm_doctor_visit_records AS dvr ON dvr.dv_client_id = client.c_id,因为那是存储医生就诊记录的表。我以为我只在 user_id 上加入了它,但显然这里出了点问题。

PS,我刚开始 OOP,所以如果我做错了,这就是我理解它的方式(尽管开放供建议)

4

2 回答 2

0

您正在执行 GROUP_CONCAT,但您没有提供 GROUP BY 子句。这将导致奇怪/错误的结果。

于 2012-11-11T22:39:58.507 回答
0

啊..解决了它,不知何故通过$client. 奇怪的是它怎么会发生。谢谢您的帮助!

于 2012-11-12T09:24:12.350 回答