将整个SQL数据库放入HTML表

将整个SQL数据库放入HTML表

我试图将整个SQL数据库放入html表中。我正在使用MySQLi API。
但它只是返回表的第一行,其余的只是看起来一团糟

            <h1> School Lesson System</h1>

            <?php

                if(isset($_SESSION['u_id'])) {
                    echo "You are logged in \n";
                }
            ?>
            <table border="1">
                <thead>
                    <tr>
                        <td>Lesson_id</td>
                        <td>Teacher</td>
                        <td>Lesson</td>
                        <td>Day</td>
                        <td>Time</td>
                        <td>Classroom</td>
                        <td>Year</td>
                        <td>Curriculum</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    require_once 'includes/dbh.inc.php';

                    $query = "SELECT * FROM monday";
                    $result = $conn->query($query);
                    $rows = $result->num_rows;
                    for ( $j = 0; $j < $rows; ++$j) {
                        $result->data_seek($j);
                        $row = $result->fetch_array(MYSQLI_ASSOC);
                    echo "<tr>";
                        echo "<td>" . $row['Lesson_id']. "</td>";
                         echo "<td>". $row['Teacher']. "</td>";
                         echo "<td>" .$row['Lesson']. "</td>";
                         echo "<td>" . $row['Day']. "</td>";
                         echo "<td>". $row['Time']. "</td>";
                         echo "<td>". $row['Classroom']. "</td>";
                         echo "<td>". $row['Year']. "</td>";
                         echo "<td>". $row['Curriculum']. "</td>";
                     echo "</tr>";
                echo"</tbody>";
            echo"</table>";
                    }


include_once 'footer.php';
?>

有什么解决办法吗????

最佳答案

1.使用while而不是for循环
2.不要关闭<tbody><table>内部回路(这是主要问题)
3.当您在代码中使用session_start();时,我无法在您的代码中看到SESSION。请检查,如果您没有,请将其添加到页面顶部。
如下所示:-

<?php
require_once 'includes/dbh.inc.php';

$query = "SELECT * FROM monday";
$result = $conn->query($query);

while($row = $result->fetch_array(MYSQLI_ASSOC)){
    echo "<tr>";
        echo "<td>" . $row['Lesson_id']. "</td>";
        echo "<td>". $row['Teacher']. "</td>";
        echo "<td>" .$row['Lesson']. "</td>";
        echo "<td>" . $row['Day']. "</td>";
        echo "<td>". $row['Time']. "</td>";
        echo "<td>". $row['Classroom']. "</td>";
        echo "<td>". $row['Year']. "</td>";
        echo "<td>". $row['Curriculum']. "</td>";
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";
include_once 'footer.php';
?>

注:-
Monday这样的表名一点也不好。这将是一个富有成果的名字,它本身描述了它的目的,如users(list of users)logs(track record of different activities)…等等。

关于php - 将整个SQL数据库放入HTML表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46212373/

10-12 17:32