2017-09-14 100 views
0

我想把整个SQL数据库放到html表中。我正在使用MySQLi API。 但它只是返回表的第一行,其余的人只是看乱七八糟up.Here是我的代码:把整个SQL数据库放到HTML表中

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

这个任何解决方案????

+2

而是用'for'循环的,只是做',而($行= $ result-> FETCH_ASSOC()){',这是标准的方式获取行。 – Qirel

+2

你有一个叫做'monday'的表?恐怕你有更大的问题需要解决! – Strawberry

回答

1

1.使用while代替for

2.不要靠近<tbody><table>内部循环(这是主要的问题)

3。当您在您的代码中使用SESSION时,我无法在您的代码中看到session_start();.So请检查,如果您没有然后将其添加到页面顶部。

做象下面这样: -

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

+0

实际上星期一表示所有lesson_id,老师,教室......在星期一发生。但是,如果你能给我一个更好的名字,我明白 –

2

那是因为你关闭了tbody和table标签里面的for循环。

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

移动这两条线以外的地方。

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>"; 
+0

大声笑!更高的声誉将我接受的答案变成最差的答案。 –

+0

它不是大声笑。 OP所面临的实际问题并不是因为'for'循环,这是因为'for'循环中写入'',这使得整个表格变形。 while循环只是编码的一个增强,但它不会解决扭曲表的实际问题。如果你耐心地与其他答案比较,那么你会发现,不仅你的答案是不正确的,而且也没有解释为什么OP必须这样做。一个答案也为未来的访问者提供服务,因此它需要有一个解释。更改你的代码使其正确,你的反对票将被删除。 –

+0

还是它的大声笑!但赞赏....我同意你的意见:) –

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语句来打印吧..

+1

为什么包装会更容易?为什么直接界面不够好?根据我的经验,像这样的包装只会产生比解决更多的麻烦。 – Qirel