Home  >  Q&A  >  body text

Rewrite the title as: Display the data in the MySQL database table as an HTML table on the web page

<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粉764836448P粉764836448447 days ago582

reply all(2)I'll reply

  • P粉252116587

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

    reply
    0
  • P粉166779363

    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>

    reply
    0
  • Cancelreply