将整个SQL数据库放入HTML表

时间:2017-09-14 06:51:57

标签: php html mysql

我试图将整个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';
?>

任何解决方案????

4 个答案:

答案 0 :(得分:2)

那是因为你在for循环中关闭了tbody和table标签。

       echo"</tbody>";
       echo"</table>";

将这两行移到for。

之外

答案 1 :(得分:1)

改变:

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

                    $query = "SELECT * FROM monday";
                    $result = $conn->query($query);
                    while ($row = $result->fetch_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>";

答案 2 :(得分:1)

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) ......等。

答案 3 :(得分:0)

创建用于连接数据库的类文件

  class DBConnUtil {
 private $mysqli;
 function __construct() {
    $this->open_connection();
}

private function open_connection() {    // function to connect database;
        $this->mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
        if ($this->mysqli->connect_error) {
            die('Error : ('. $this->mysqli->connect_errno .') '. $this->mysqli->connect_error);
        }
        $this->mysqli->set_charset("utf8");
}
public function run_query ($queryString) {
    $result=$this->mysqli->query($queryString);

    if (!$result) {
        trigger_error('Wrong SQL: ' . $queryString . ' Error: ' . $this->mysqli->error, E_USER_ERROR);                                                                  
    }
    $this->close_connection();
    return $result;
}



private function close_connection() {
        $this->mysqli->close();
}    

}

然后是另一个类文件Lesson.php。使用数据库列作为私有变量。然后像这样写一个函数fetchData()。

    public function fetchData() {
    $dbConn = new DBConnUtil(); //name of the database class mention 
    before..
    $queryString = "select * from tablename ";
    $result = $dbConn->run_query($queryString);
    $rows_returned = $result->num_rows;

    $categorydata = array();
    while($rows = $result->fetch_object()){
   $categorydata[] =  $rows;
    }
    $result->free();
   return $categorydata;

   }  

然后使用foreach语句打印它..