简体   繁体   English

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

[英]How do I display all tables in a database as HTML tables?

I have selected values from a single MySQL table and displayed them in a HTML table many times before using PHP, like this:在使用 PHP 之前,我从单个 MySQL 表中选择了值并将它们显示在 HTML 表中多次,如下所示:

$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.谢谢。

Updated Answer:更新答案:

Try the following code试试下面的代码

<?php

require_once('includes/api/db-config.php');
$db = Database::getInstance();
$conn = $db->getConnection();
$dbname = 'databaseName';

$sql = "SHOW TABLES FROM {$dbname}";
$result = mysqli_query($conn,$sql);

if (!$result) {
    echo 'MySQL Error: ' . mysqli_error($conn);
    exit;
}

while ($row = mysqli_fetch_row($result)) {
    $tableSql = "SELECT * FROM {$row[0]}";
    $tableResult = mysqli_query($conn,$tableSql);
    $Response = "<tr>";
    while ($tableRow = mysqli_fetch_row($tableResult)) {
        $Response .= "<td>{$tableRow["id"]}</td>";
        $Response .= "<td>{$tableRow["title"]}</td>";
        $Response .= "<td>{$tableRow["description"]}</td>";
        $Response .= "<td>{$tableRow["action"]}</td>";
        $Response .= "<td>{$tableRow["mods"]}</td>";
        $Response .= "<td>{$tableRow["date"]}</td>";
    }
    $Response .= "</tr>";

}

?>

Now you have data from all the tables one by one now you can display the data in html table.现在您拥有所有表格中的数据,现在您可以在 html 表格中显示数据。

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:如果您想遍历所有表格并在 HTML 表格中列出每个表格的数据,包括显示列名,那么这应该适合您:

//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;
    }
}

this works for me这对我有用

require_once('db.php');

   $dbname = 'sport';

   $sql = "SHOW TABLES FROM $dbname"; $result =
   mysqli_query($conn,$sql);

   if (!$result) {
       echo "DB Error, could not list tables\n";
       echo 'MySQL Error: ' . mysqli_error($conn);
       exit; }

   while ($row = mysqli_fetch_row($result)) {   //print_r($row);
       echo "Table: {$row[0]}\n"; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示数据库中的表? - How do I display tables from a database? 如何使用php将mysql数据库中的所有表显示或显示到html表中 - How to display or show all tables in a mysql database into html table with php SQL(如何显示所有表的所有结果?) - SQL (How do I display all results from all tables?) 如何在php中显示数据库中其他表的值? - How do I display values from other tables in a database in php? 如何在两个单独的html5表上显示来自具有不同数据量的两个表的数据? - How do i display data from two tables with different amount of data on tow separate html5 tables? 我可以显示 MySQL 数据库中的所有表和数据吗? - Can I display all tables and data in a MySQL database? 我如何连接7个表(其中所有表中都存在列)等于所有表中相同内容的表? - how do i join 7 tables where a column (which exists in all tables) equals the same content in all tables? 如何使用php mysqli搜索数据库的所有表并显示答案 - how to search all the tables of the database using php mysqli and display the answer 我在 php 中执行此代码时选择任何数据库它只显示表的数量而不是所选数据库的所有表的名称 - I am doing this code in php while selecting any database it only display the number of tables not the names of all tables of the selected database 如何显示内部联接表中的必填字段 - How do i display required fields from inner joined tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM