P粉2521165872023-08-21 12:31:52
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粉1667793632023-08-21 11:29:13
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>