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