简体   繁体   English

用php和mysqli创建表

[英]Table creation with php and mysqli

i'm trying to do a table with php which takes the data from my database and build's the table im html but despite my while loop , only the 1st row is getting into the table. 我试图用php做一个表,该表从我的数据库中获取数据并建立html的表,但是尽管有while循环,但只有第一行进入该表。 If i change the while loop before the 如果我在之前更改while循环

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

I get all of them but in 3 tables. 我得到了所有这些,但只有3张桌子。

What's wrong with the code ? 代码有什么问题?

<?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>";
        };

 ?>

Move echo "</table>"; 移动echo "</table>"; out of the while loop- while循环之外

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

For each row you are adding the closing </table> tag which should not be like that. 对于每一行,您都添加了结束</table>标记,该标记应该不是这样。 echo "</table>"; code must be out of the while loop. 代码必须不在while循环中。 It must be written once only. 它只能写一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM