没有数据显示MySql php

时间:2017-07-11 02:43:51

标签: php mysql sql database

我现在遇到的问题是,如果My MySql表中包含数据,我将获得重复数据。但是,如果我的一个表为空,则无法显示所有数据。

这是我的代码:

$stmt = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
        . "JOIN employementdetails ed ON apd.ApplicantID = ed.ApplicantID "
        . "JOIN sourceoffunds sof ON apd.ApplicantID = sof.ApplicantID "
        . "JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
        . "WHERE apd.AccountID ='{$accountId}' AND applicantType ='joint1';");        

$stmt->execute();

if ($stmt->rowCount() > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);

        echo $row['EmploymentStatus'];
        ?>
        <?php
    }
} else {
    ?>
    <div class="">
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
        </div>
    </div>
    <?php
} 

1 个答案:

答案 0 :(得分:2)

如果您想要所有行,请使用LEFT JOIN,即使是不匹配的行:

SELECT *
FROM applicantpersonaldetails apd LEFT JOIN
     employementdetails ed
     ON apd.ApplicantID = ed.ApplicantID LEFT JOIN
     sourceoffunds sof
     ON apd.ApplicantID = sof.ApplicantID LEFT JOIN
     existingbankproducts ext
     ON apd.ApplicantID = ext.ApplicantID
WHERE apd.AccountID ='{$accountId}' AND applicantType = 'joint1';

注意:不清楚哪个表applicantType来自哪个。如果它不是来自apd,那么您可能希望将其包含在相应的ON子句中,而不是WHERE子句中。