Html Table 错误

Html Table Error

此代码显示每一行的标题。我的错误是什么?

foreach($db->query('SELECT * FROM uyeler') as $row) {

            echo "<table>
                    <tr>
                    <th>ID</th>
                    <th>Username</th>
                    <th>Sex</th>
                    <th>Country</th>
                    <th>Age</th>
                    <th>Twitter</th>
                    <th>Instagram</th>
                    <th>Snapchat</th>
                    </tr>";

            echo "<tr><td>" .$row['id'] . "</td>";
            echo "<td>" .$row['username'] . "</td>";
            echo "<td>" .$row['sex'] . "</td>";
            echo "<td>" .$row['country'] . "</td>";
            echo "<td>" .$row['age'] . "</td>";
            echo "<td>" .$row['twitter'] . "</td>";
            echo "<td>" .$row['instagram'] . "</td>";
            echo "<td>" .$row['snapchat'] . "</td>";

        echo "</tr></table>";
        }

table好像是这样

ID  Username    Sex Country Age Twitter Instagram   Snapchat

1   canozpey    male    sadsad  0   twit    insta   snap

ID  Username    Sex Country Age Twitter Instagram   Snapchat

2   s   male    Russia  12  test2   sad jgfhf

ID  Username    Sex Country Age Twitter Instagram   Snapchat

3   sda male    male    6   male    male    male

ID  Username    Sex Country Age Twitter Instagram   Snapchat

4   asd female  sadasd  0       

ID  Username    Sex Country Age Twitter Instagram   Snapchat

5   adsafa  female  dassd   0           
    <table>
                        <tr>
                        <th>ID</th>
                        <th>Username</th>
                        <th>Sex</th>
                        <th>Country</th>
                        <th>Age</th>
                        <th>Twitter</th>
                        <th>Instagram</th>
                        <th>Snapchat</th>
                        </tr>
<?php
    foreach($db->query('SELECT * FROM uyeler') as $row) {
       <td><?php echo $row['id'] ?></td>
       <td>echo $row['username'] </td>
       <td>echo $row['sex'] </td>
       <td>echo $row['country'] </td>
       <td>echo $row['age'] </td>


    }?>
</table>

您需要将包装和 header 放在循环之外 - 请注意,您只是在内容上循环,因此您不会在 foreach.[=12= 中包含这些标签]

<table>
  <tr>
    <th>ID</th>
    <th>Username</th>
    <th>Sex</th>
    <th>Country</th>
    <th>Age</th>
    <th>Twitter</th>
    <th>Instagram</th>
    <th>Snapchat</th>
</tr>

<?php
foreach($db->query('SELECT * FROM uyeler') as $row) {
        echo "<tr><td>" .$row['id'] . "</td>";
        echo "<td>" .$row['username'] . "</td>";
        echo "<td>" .$row['sex'] . "</td>";
        echo "<td>" .$row['country'] . "</td>";
        echo "<td>" .$row['age'] . "</td>";
        echo "<td>" .$row['twitter'] . "</td>";
        echo "<td>" .$row['instagram'] . "</td>";
        echo "<td>" .$row['snapchat'] . "</td>";
        echo "</tr>";
    }
?>

</table>