将标题重写为:将MySQL数据库表中的数据在网页上以HTML表格的形式展示
P粉764836448
P粉764836448 2023-08-20 14:48:09
0
2
448
<p>我想从数据库表中检索值,并在页面中以html表格的形式显示出来。 我已经搜索过了,但是找不到答案,尽管这肯定是一件容易的事情(这应该是数据库的基础知识哈哈)。我猜我搜索的术语可能是误导性的。 数据库表的名称是tickets,它现在有6个字段(submission_id、formID、IP、name、email和message),但应该还有一个名为ticket_number的字段。 我该如何使其以以下html表格的形式显示数据库中的所有值:</p> <pre class="brush:php;toolbar:false;">&lt;table border="1"&gt; &lt;tr&gt; &lt;th&gt;Submission ID&lt;/th&gt; &lt;th&gt;Form ID&lt;/th&gt; &lt;th&gt;IP&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;E-mail&lt;/th&gt; &lt;th&gt;Message&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;123456789&lt;/td&gt; &lt;td&gt;12345&lt;/td&gt; &lt;td&gt;123.555.789&lt;/td&gt; &lt;td&gt;John Johnny&lt;/td&gt; &lt;td&gt;johnny@example.com&lt;/td&gt; &lt;td&gt;This is the message John sent you&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;</pre> <p>然后在'john'下面显示所有其他值。</p>
P粉764836448
P粉764836448

membalas semua(2)
P粉252116587

尝试这个:(完全动态...)

<?php
$host    = "localhost";
$user    = "username_here";
$pass    = "password_here";
$db_name = "database_name_here";

//创建连接
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = mysqli_connect($host, $user, $pass, $db_name);

//从数据库获取结果
$result = mysqli_query($connection, "SELECT * FROM products");

//显示属性
echo '<table class="data-table">
        <tr class="data-heading">';  //初始化表格标签
while ($property = mysqli_fetch_field($result)) {
    echo '<td>' . htmlspecialchars($property->name) . '</td>';  //获取字段名称作为表头
}
echo '</tr>'; //结束tr标签

//显示所有数据
while ($row = mysqli_fetch_row($result)) {
    echo "<tr>";
    foreach ($row as $item) {
        echo '<td>' . htmlspecialchars($item) . '</td>'; //获取项目
    }
    echo '</tr>';
}
echo "</table>";
P粉166779363

先获取数据,然后稍后显示。

<?php
$con = mysqli_connect("localhost","peter","abc123","my_db");
$result = mysqli_query($con,"SELECT * FROM Persons LIMIT 50");
$data = $result->fetch_all(MYSQLI_ASSOC);
?>

<table border="1">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <?php foreach($data as $row): ?>
  <tr>
    <td><?= htmlspecialchars($row['first_name']) ?></td>
    <td><?= htmlspecialchars($row['last_name']) ?></td>
  </tr>
  <?php endforeach ?>
</table>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!