Rewrite the title as: Display the data in the MySQL database table as an HTML table on the web page
P粉764836448
P粉764836448 2023-08-20 14:48:09
0
2
585
<p>I want to retrieve values ​​from a database table and display them in a page as an html table. I've searched but can't find the answer, although it's certainly an easy thing to do (it should be database basics haha). I guess the terms I searched for might be misleading. The name of the database table is tickets, it now has 6 fields (submission_id, formID, IP, name, email and message), but there should be another field called ticket_number. How can I make it display all the values ​​from the database in the form of the following html table: </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>Then display all other values ​​below 'john'. </p>
P粉764836448
P粉764836448

reply all(2)
P粉252116587

Try this: (fully dynamic...)

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

Get the data first and then display it later.

<?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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template