为什么mysqli_fetch_row会阻止我的所有结果显示?

时间:2016-05-31 22:28:27

标签: php mysql

我正在进行一项调查跟踪网站,我遇到了麻烦。我想显示过去7天内完成的所有调查。我正在使用mysqli_fetch_row来查看是否有任何行被检索,以及是否显示它们。如果他们在过去7天内没有被强制执行,我希望它显示“最近没有强制要求的调查显示”。

<?php
            require('db/connect.php');
            if (!isset($_GET['sort'])) {
                $sort = 'client_id';
            } else {
                $sort = $_GET['sort'];
            }
            if ($result = $db->query("SELECT client_id, date_added, client, email, date_sent, date_completed FROM clients NATURAL JOIN surveys WHERE date_completed BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() ORDER BY $sort")) {//shows surveys completed in the last 7 days
                if (mysqli_fetch_row($result) == 0) {
                    echo "No recently completed surveys to show.";
                } else {
                    echo "<table>";
                    echo "<tr><th><a href='portal.php?sort=client_id'>ID</a></th><th><a href='portal.php?sort=date_added'>Date Added</a></th><th><a href='portal.php?sort=client'>Client</a></th><th><a href='portal.php?sort=email'>Email</a></th><th><a href='portal.php?sort=date_sent'>Sent</a></th><th><a href='portal.php?sort=date_completed'>Completed</a></th>";
                        $rows = $result->num_rows;
                        for ($num = 0; $num < $rows; ++$num) {
                            $row = $result->fetch_array(MYSQLI_NUM);
                                $client_id = $row[0];
                                $date = $row[1];
                                $client = $row[2];
                                $email = $row[3];
                                $sent = $row[4];
                                $completed = $row[5];
                                echo "<tr>";
                                echo "<td>$client_id</td>";
                                echo "<td>$date</td>";
                                echo "<td>$client</td>";
                                echo "<td>$email</td>";
                                echo "<td>$sent</td>";
                                echo "<td><a href='survey/completed/index.php?id=$client_id'>$completed</a></td>";
                                echo "</tr>";
                            }
                            echo "</table>";
                        } 
                    }
        ?>

当我删除mysqli_fetch_row的if子句时,它会显示所有最近完成的调查,但如果我将其保留,它总是会留下一个。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您在进行测试时丢弃第一个。你可以改为获得这样的行数:

if ($result->num_rows == 0) {
    echo "No recently completed surveys to show.";
} else {
   // ....
}

文档:mysqli_result::$num_rows