未显示第一行html

时间:2018-09-06 14:00:54

标签: php mysql

因此,假设我的数据库中有10行。但是,在mysql查询后,html表中仅显示9行。 我需要数据库中的每一行都显示在html表中。

我希望有人能指出我正确的方向。如果需要提供更多详细信息,我会在这里。

这是我的代码:

<?php
$sqlQuery = "SELECT * FROM periods";
$result = mysqli_query($conn, $sqlQuery);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['id'];
        $out = $row['fell_out'];
        $in = $row['fell_in'];
        $sum = $row['sum'];
        $nextEstimate = $row['next_estimate'];
        $nextEstimateDays = $row['next_estimate_days'];
        $notes = $row['notes'];

        $sqlQueryLastDate = "SELECT * FROM (select * from periods WHERE id < $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
        $resultLastDate = mysqli_query($conn, $sqlQueryLastDate);
        $resultCheckLastDate = mysqli_num_rows($resultLastDate);
        if ($resultCheckLastDate > 0) {
            while ($rowLastDate = mysqli_fetch_assoc($resultLastDate)) {
                $lastInDate = $rowLastDate['fell_in'];

                $sqlQueryCurrentDate = "SELECT * FROM (select * from periods WHERE id = $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
                $resultCurrentDate = mysqli_query($conn, $sqlQueryCurrentDate);
                $resultCheckCurrentDate = mysqli_num_rows($resultCurrentDate);
                if ($resultCheckCurrentDate > 0) {
                    while ($rowCurrentDate = mysqli_fetch_assoc($resultCurrentDate)) {
                        $currentOutDate = $rowCurrentDate['fell_out'];

                        $lastIn = new DateTime($lastInDate);
                        $currentOut = new DateTime($currentOutDate);
                        $intervalLastCurrent = $lastIn->diff($currentOut);
                        $elapsedLastCurrent = $intervalLastCurrent->format('%a days %h hours');
                        /*Why? Php is erasing everything after adding the above variable to the table...Entire first row gets erased!*/
                        echo "
                                <tr>    
                                    <td>".$id."</td>
                                    <td class='test'>".$elapsedLastCurrent."</td> 
                                    <td class='dateOutResult'>".$out."</td>
                                    <td class='dateInResult'>".$in."</td>
                                    <td class='sumHours'>".$sum."</td>
                                    <td class='nextEstimate'>".$nextEstimate." (".$nextEstimateDays.")</td>
                                    <td class='notes'>".$notes."</td>
                                </tr>";
                    } /*$sqlQueryCurrentDate*/
                }
            } /*$sqlQueryLastDate*/
        }
    } /*$sqlQuery*/
}
?>

实时结果如下:https://unidrones.co.za/periods

我真的是mysql / php的新手,所以我不明白我在做什么错。嵌套查询可能不是最好的使用方法,但我仍在学习中,任何输入都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

这是因为您的查询是 //从ID <$ id ORDER BY ID DESC LIMIT 1的句点中选择*; 您的第一个$ id将始终等于id。