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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?