drf65218 2017-09-14 06:51
浏览 114
已采纳

将整个SQL数据库放入HTML表

I'm trying to put a whole SQL database into html table. I'm using MySQLi API. But it just return the first row of the table , and the rest of them just look mess up.Here's my code:

            <h1> School Lesson System</h1>

            <?php

                if(isset($_SESSION['u_id'])) {
                    echo "You are logged in 
";
                }
            ?>
            <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';
?>

Any solution for this ????

  • 写回答

4条回答 默认 最新

  • doumanshan6314 2017-09-14 06:56
    关注

    1.Use while instead of for loop

    2.don't close <tbody> and <table> inside loop (which is the main problem)

    3.I am unable to see session_start(); in your code, while you are using SESSION in your code.So please check and if you don't have then add that on top of the page.

    Do like below:-

    <?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';
    ?>
    

    Note:-

    Table names like Monday is not good at all. It will be fruitful name which describes it's purpose itself like users(list of users),logs(track record of different activities)...etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀