while循环表

时间:2015-06-17 06:38:26

标签: php sql while-loop

我有这个while循环的问题。我想创建一个管理员可以看到用户并删除它们的表。所以我想把它变成一张桌子。我尝试了一些回声:

echo <table>, echo <td>, echo </td>, echo </table>.

不幸的是,没有任何可行的方法。 有谁能够帮我?

<?php
                    $account = 'Account:';
                    $password1 = 'Password:';
                        //check db connection
                        if ($conn->connect_error) {
                            die("Connection failed: " . $conn->connect_error);
                        } 
                        // Take everything from table and fill in $result
                        $sql = "SELECT * FROM login";
                        $result = $conn->query($sql);

                        if ($result->num_rows > 0) {
                            // Take all data
                            while($row = $result->fetch_assoc()) {
                                //
                                echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
                            }
                        } else {
                            // nothing in DB is 0 results
                            echo "0 results";
                        }
                        $conn->close();




                        ?>

4 个答案:

答案 0 :(得分:1)

只需打印table -

即可
<table>
<tr>
    <td>Account</td>
    <td>Password</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
    echo "<tr><td>" .$row["username"]. "</td><td>" . $row["password"]. "</td></tr>";
}
?>
</table>

答案 1 :(得分:0)

                   $sql = "SELECT * FROM login";
                    $result = $conn->query($sql);
                    echo "<table>";
                        echo "<tr>";

                        echo '<td>Username</td>';

                        echo '<td>Password</td>';
                        echo '<td>Action</td>';
                        echo "</tr>";
                    if ($result->num_rows > 0) {
                        // Take all data
                        while($row = $result->fetch_assoc()) {
                            //
                            echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
                            echo '<tr>';

                            echo '<td>'.$row["username"].'</td>';
                            echo '<td>'.$password1.'</td>';

                            echo '<td><a href="path/to/youfile.php?userid='.$row['id'].'">Delete</a></td>';
                            echo '</tr>';
                        }
                    } else {
                        // nothing in DB is 0 results
                        echo "0 results";
                        echo '<tr><td colspan="3" rowspan="" headers="">No records Found</td></tr>';

                    }
                    echo "</table>";
                    $conn->close();

答案 2 :(得分:0)

<?php
                    $account = 'Account:';
                    $password1 = 'Password:';
                        //check db connection
                        if ($conn->connect_error) {
                            die("Connection failed: " . $conn->connect_error);
                        } 
                        // Take everything from table and fill in $result
                        $sql = "SELECT * FROM login";
                        $result = $conn->query($sql);

                        echo"<table><tr><td>Username</td><td>Password</td><td>Action</td></tr>";

                        if ($result->num_rows > 0) {
                            // Take all data
                            while($row = $result->fetch_assoc()) {

                                echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td> modify | delete </td></tr>";

                            }
                        } else {
                            // nothing in DB is 0 results
                            echo "0 results";
                        }
                        echo"</table>";
                        $conn->close();




                        ?>

答案 3 :(得分:0)

用以下内容替换你的while循环:

    echo "<table><tr>
    <td>Account</td>
    <td>Username</td>
    <td>Password</td>
</tr>"
    while($row = $result->fetch_assoc()) 
    {
        echo "<tr><td>".$account."</td><td>" .$row["username"]. "</td><td>" . $password1 . "</td><td>" . $row["password"]. "</td></tr>";
    }
    echo "</table>";