我正在尝试用php做一个表格,该表格从我的数据库中获取数据并构建html表格,但尽管有while循环,但只有第一行进入了表格。如果我在之前更改while循环

echo "<table border='1' cellpadding='2' cellspacing='2'";


我得到了所有这些,但只有3张桌子。

代码有什么问题?

<?php
    require 'connect.php';

    $query = $link->query("SELECT * FROM fornecedor");

    echo "Fornecedores";

    echo "<table border='1' cellpadding='2' cellspacing='2'";

    echo "<tr>
            <td>Nome</td>
            <td>NIF</td>
            <td>Cidade</td>
            <td>Rua</td>
            <td>NrPorta</td>
            <td>Website</td>
            <td>email</td>
        </tr>";
        while ($row = mysqli_fetch_array($query)) {

            echo "<tr>";
            echo "<td>" . $row["Nome"] . "</td>";
            echo "<td>" . $row["NIF"] . "</td>";
            echo "<td>" . $row["Cidade"] . "</td>";
            echo "<td>" . $row["Rua"] . "</td>";
            echo "<td>" . $row["NrPorta"] . "</td>";
            echo "<td>" . $row["Website"] . "</td>";
            echo "<td>" . $row["Email"] . "</td>";
            echo "</tr>";
            echo "</table>";
        };

 ?>

最佳答案

echo "</table>";while循环中移出-

....
while(){
   .....
   // don't close the table tag here
}
echo "</table>";

10-06 08:26