php - 在网页上的 HTML 表中显示 MySQL 数据库表中的值

标签 php mysql database html-table

我想从数据库表中检索值并将它们显示在页面的 html 表中。 我已经搜索过这个但我找不到答案,虽然这肯定很容易(这应该是数据库的基础知识大声笑)。我想我搜索过的术语具有误导性。 数据库表名是 tickets,它现在有 6 个字段(submission_id、formID、IP、name、email 和 message),但应该还有一个名为 ticket_number 的字段。 我怎样才能让它在这样的 html 表中显示来自数据库的所有值:

<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>

然后是“john”下面的所有其他值。

最佳答案

示例取自 W3School:PHP Select Data from MySQL

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

这是一个学习的好地方!

关于php - 在网页上的 HTML 表中显示 MySQL 数据库表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56654653/

相关文章:

php - 如何在我的表中获取最后一个 reasonID?

php - mysql查询更新字段错误

php - 如何在之前的 MySQL 操作中获取受影响的行?

mysql - 多个否定子查询的更好方法

PHP/HTML/JavaScript : Automatically leave page?

python - mac osx 10.9.2 : error: command '/usr/bin/clang' failed with exit status 1 上的 mysql-python

php - 学说迁移 : altering a column to be nullable in production

sql - C# 访问和显示表中数据的方法

Python 编程错误 : Column Does Not Exist

mysql - 如何将table1中的table2更新为table2中的相应条目?