用于在表中动态获取数据的pdo代码

时间:2017-03-27 11:16:47

标签: pdo fetchall

我想要一个pdo代码,用于在打开页面时动态地将数据从数据库中提取到表中。我花了整整一天但我仍然没有得到合适的代码,请帮助我

<div class="card">
            <div class="card-header">
                <h2>Selection Example <small>Ensure that the data attribute [data-identifier="true"] is set on one column header.</small></h2>
            </div>

            <table id="data-table-command" class="table table-striped table-vmiddle">
                <thead>
                    <tr>
                        <th data-column-id="id" data-type="numeric">ID</th>
                        <th data-column-id="sender">Sender</th>
                        <th data-column-id="received" data-order="desc">Received</th>
                        <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
                    </tr>
                </thead>
                <tbody>
                                            <tr>
                        <td>10238</td>
                        <td>eduardo@pingpong.com</td>
                        <td>14.10.2013</td>
                    </tr>
                    <tr>
                        <td>10243</td>
                        <td>eduardo@pingpong.com</td>
                        <td>19.10.2013</td>
                    </tr>
                    <tr>
                        <td>10248</td>
                        <td>eduardo@pingpong.com</td>
                        <td>24.10.2013</td>
                    </tr>
                    <tr></tbody>
            </table>
        </div></div>

1 个答案:

答案 0 :(得分:0)

$data = $pdo->query("SELECT * FROM t")->fetchAll();
if ($data) {
    echo "<table><tr>";
    foreach (array_keys($data[0] as $col)) {
        echo "<th>$col</th>";
    }
    echo "</tr>";
    foreach($pdo->query("SELECT * FROM t") as $row) {
        echo "<tr>";
        foreach ($row as $col) {
            echo "<td>$col</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}