使用PDO将结果打印到表中

时间:2013-10-03 10:50:28

标签: php pdo html-table

我正在使用PDO而我正试图在桌面上打印结果,但这些值不会出现。我需要使用<td>代码。

这是我在弗朗西斯科的帮助下的完整代码:

 <?php
    require_once('config.php');


    $stmt = $connect->prepare("SELECT id, username, email FROM user");
    $stmt->execute();  
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    ?>  


                <table id="table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Username</th>
                            <th>email</th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                       foreach ($result as $row): 

                            ?>
                            <tr>
                                <td><?= $row['id'];?></td>
                                <td><?= $row['username'];?></td>
                                <td><?= $row['email'];?></td>

                            </tr>
                            <?php
                        endforeach;
                        ?>
                    </tbody>
                </table>

1 个答案:

答案 0 :(得分:0)

您需要在每次迭代中在循环之外调用fetchAll(),如下所示:

fetch()

如果你想要foreach,只需调用while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) // do sth 并对结果进行foreach循环。

相关问题