dspvin19712 2018-11-05 10:19
浏览 123
已采纳

如何将数据库中的所有表显示为HTML表?

I have selected values from a single MySQL table and displayed them in a HTML table many times before using PHP, like this:

$query = "SELECT * FROM main";

$result = $connection->query($query);

while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  echo "<td>" . $row['row1'] . "</td>";
  echo "<td>" . $row['row2'] . "</td>";
  echo "<td>" . $row['row3'] . "</td>";
  echo "<td>" . $row['row4'] . "</td>";
}

How can I display all of the tables in a certain database in the same way? I need it to be dynamic, so that if new tables are added to that database, they will show up on the webpage as well.

This is something I have tried, that doesn't seem to work. Perhaps I can get some feedback?

$query1 = "SHOW TABLES FROM db_name";

// This is equal to the number of tables in the database.
$query2 = "SELECT COUNT(*) FROM main";

$result1 = $connection->query($query1);

$result2 = $connection->query($query2);

$row2 = mysqli_fetch_assoc($result2);

$count = $row2["COUNT(*)"];

$counter = 1;

while ($row1 = mysqli_fetch_array($result1)) {
        ${getter.$counter++} = "SELECT * FROM " . $row[0];
    }

<table>
        <?php
        for ($i = 1; $i <= $count; $i++) {
                ${request.$i} = $connection->query(${getter.$i});
                while ($row3 = mysqli_fetch_assoc(${request.$i})) {
                        echo "<tr>";
                        echo "<td>" . $row['row1'] . "</td>";
                        echo "<td>" . $row['row2'] . "</td>";
                        echo "<td>" . $row['row3'] . "</td>";
                        echo "<td>" . $row['row4'] . "</td>";
                }
        }
        ?>
</table>

Thanks.

  • 写回答

3条回答 默认 最新

  • duanji2772 2018-11-05 11:29
    关注

    If you want to loop through all your tables and list the data from each one in a HTML table, including showing the column names, then this should work for you:

    //get all the tables in the database
    $sql = "SHOW TABLES FROM db_name";
    $result = $connection->query($sql);
    
    if ($result === false) die($conn->error);
    
    //loop through the list of tables
    while ($row = $result->fetch_row()) {
        echo "<h2>Table: ".$row[0]."</h2>";
    
        //now, for the current table, get all the data and loop through the rows
        $sql2 = "SELECT * FROM ".$row[0];
        $result2 = $connection->query($sql2);
    
        if ($result2)
        {
            //get the column names
            $fields = $result2->fetch_fields();
            echo "<table><thead><tr>";
    
            //loop through the field names and output each one as a column heading
            foreach ($fields as $fld)
            {
              echo "<th>".$fld->name."</th>";
            }
            echo "</tr></thead><tbody>";
    
            //get the rows
            while ($row2 = $result2->fetch_row()) {
                echo "<tr>";
    
                //loop through each data value in the row and output into a HTML table cell
                foreach ($row2 as $cell) {
                    echo "<td>".$cell."</td>";
                }
                echo "</tr>";
            }
    
            echo "</tbody></table><hr>";
        }
        else
        {
          echo "Problem retrieving data for ".$row[0].": ".$conn->error;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计