将完整的MySQL表加载到HTML页面中

时间:2015-05-24 06:09:45

标签: php html mysql

所以我正在尝试创建一个基本的发布系统,页面上有一个表单,当我提交时在我的MySQL数据库上创建了一行并且我已经成功完成了。如何使用PHP将数据库中的所有数据加载到HTML表中?

2 个答案:

答案 0 :(得分:2)

@Andrew所说的实现:

<?php
$mysqli = new mysqli("host", "username", "password", "db");

if ($mysqli->connect_errno)
    die("Connect failed: {$mysqli->connect_error}");

$table = "";
if ($result = $mysqli->query('SELECT * FROM yourTable;')) {
    // create the table header row
    $fieldsInfo = $result->fetch_fields();
    $table .= "<table border='1'><tr>";
    foreach($fieldsInfo as $fieldInfo)
        $table .= "<th>{$fieldInfo->name}</th>";
    $table .= "</tr>";

    // create the table content rows
    while ($row = $result->fetch_assoc()) {
        $table .= "<tr>";
        foreach($row as $columnValue)
            $table .= "<td>$columnValue</td>";
        $table .= "</tr>";
    }
    $table .= "</table>";
}

echo $table;
?>

答案 1 :(得分:0)

非常广泛的问题,所以我会给你一个广泛的答案:你会查询数据库,遍历数据结果集,并可能将数据显示为html表中的行。