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

时间:2018-11-05 10:19:40

标签: php mysql database

在使用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>";
}

如何以相同的方式显示某个数据库中的所有表?我需要它是动态的,以便将新表添加到该数据库中,它们也将显示在网页上。

这是我尝试过的方法,似乎不起作用。也许我可以得到一些反馈?

$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>

谢谢。

3 个答案:

答案 0 :(得分:1)

最新答案:

尝试以下代码

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

}

?>

现在您可以一一列出所有表中的数据,现在可以在html表中显示数据了。

答案 1 :(得分:0)

如果您要遍历所有表并在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;
    }
}

答案 2 :(得分:-1)

这对我有用

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