从MySql / PHP为每个ROW创建新的HTML表

时间:2018-12-17 20:27:44

标签: php html mysql

任何人都可以帮助我将下面的每一行放入单独的html表中。

那么每行有一张表,而不是整个数据库只有一张表?

最好通过int ID进行调用并以此方式填充吗?



我有一个数据库“ dorav3 ”,表名称为“ dtab1 ”,其以下各列

+---------------+---------+------+-----+---------+----------------+
| Field         | Type    | Null | Key | Default | Extra          |
+---------------+---------+------+-----+---------+----------------+
| Pillar        | text    | YES  |     | NULL    |                |
| PD Team       | text    | YES  |     | NULL    |                |
| PD Team ID    | text    | YES  |     | NULL    |                |
| PD Team URL   | text    | YES  |     | NULL    |                |
| Summary       | text    | YES  |     | NULL    |                |
| PD Service    | text    | YES  |     | NULL    |                |
| PD Service ID | text    | YES  |     | NULL    |                |
| Outlook DL    | text    | YES  |     | NULL    |                |
| Outlook Email | text    | YES  |     | NULL    |                |
| Description   | text    | YES  |     | NULL    |                |
| Tags          | text    | YES  |     | NULL    |                |
| PD EP ID      | text    | YES  |     | NULL    |                |
| PD EP URL     | text    | YES  |     | NULL    |                |
| id            | int(11) | NO   | PRI | NULL    | auto_increment |
+---------------+---------+------+-----+---------+----------------+

我可以使用以下内容连接到HTML并从中填充HTML。

PHP

<?php

// php populate html table from mysql database
$hostname = "localhost";
$username = "xxxx";
$password = "xxxx";
$databaseName = "dorav3";



// connect to mysql
$connect = mysqli_connect($hostname, $username, $password, $databaseName);



// mysql select query
$query = "SELECT * FROM `dtab1";



$result1 = mysqli_query($connect, $query);




$dataRow = "";

while($row2 = mysqli_fetch_array($result2))
{
    $dataRow = $dataRow."<tr><td>$row2[0]</td><td>$row2[1]</td><td>$row2[2]</td><td>$row2[3]</td><td>$row2[4]</td><td>$row2[5]</td><td>$row2[6]</td><td>$row2[7]</td><td>$row2[8]</td><td>$row2[9]</td><td>$row2[10]</td><td>$row2[11]</td><td>$row2[12]</td></tr>";
}

?>

HTML

<title>PHP DATA ROW TABLE FROM DATABASE</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>

    table,th,tr,td
    {
        border: 1px solid black;
    }

</style>

<table>

    <tr>
        <th>Pillar</th>
        <th>PD Team</th>
        <th>PD Team ID</th>
        <th>PD Team URL</th>
        <th>Summary</th>
        <th>PD Service</th>
        <th>PD Service ID</th>
        <th>Outlook DL</th>
        <th>Outlook Email</th>
        <th>Description</th>
        <th>Tags</th>
        <th>PD EP ID</th>
        <th>PD EP URL</th>
    </tr>

    <?php while($row1 = mysqli_fetch_array($result1)):;?>
    <tr>
        <td><?php echo $row1[0];?></td>
        <td><?php echo $row1[1];?></td>
        <td><?php echo $row1[2];?></td>
        <td><?php echo $row1[3];?></td>
        <td><?php echo $row1[4];?></td>
        <td><?php echo $row1[5];?></td>
        <td><?php echo $row1[6];?></td>
        <td><?php echo $row1[7];?></td>
        <td><?php echo $row1[8];?></td>
        <td><?php echo $row1[9];?></td>
        <td><?php echo $row1[10];?></td>
        <td><?php echo $row1[11];?></td>
        <td><?php echo $row1[12];?></td>
    </tr>
    <?php endwhile;?>

</table>

1 个答案:

答案 0 :(得分:-3)

您应该将while标记放在table标记之前,将endwhile标记放在关闭table标记之后。