-2

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.

6
  • 2
    so what you're really asking is how to ask mysql what tables exist in the database. after that, it's just a case of repeating your current process multiple times (loop through all the tables, for each one, query all the data for them, make a table, output it). Have you googled how to ask mysql what tables exist in a database? Once you know that, the solution is pretty trivial.
    – ADyson
    Nov 5, 2018 at 10:23
  • @ADyson I've just updated my original post with something I've tried that doesn't work, but I guess I am having trouble with the "loop through and output" part. Nov 5, 2018 at 10:32
  • Thanks. What exactly isn't working? What output are you seeing? I'd expect you're having a problem with the field names, maybe? Since you don't know what the field names of each table will be, it would make more sense to fetch a non-associative array, and just loop through the fields until you reach the end of the row.
    – ADyson
    Nov 5, 2018 at 10:35
  • @ADyson Nothing is displayed on the page. In the error_log, it says "Use of undefined constant get" and "Use of undefined constant request". Nov 5, 2018 at 10:41
  • It's because of this code ${request.$i} (I mean for the second error, for example). ...it thinks request is some kind of constant variable. It's not very clear to me what you were trying to achieve with this code TBH, it seems to be rather over-complicating things.
    – ADyson
    Nov 5, 2018 at 10:59

3 Answers 3

1

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.

7
  • Kindly add your database structure Nov 5, 2018 at 10:55
  • It's MySQL. For clarification, I need all of the rows of all of the tables in one database to be displayed in HTML. Nov 5, 2018 at 10:59
  • No! Kindly share your table structure! Nov 5, 2018 at 11:00
  • @UssaidIqbal It's not entirely clear, but I think the whole point is to be able to list all of the tables and data without knowing the structure in advance.
    – ADyson
    Nov 5, 2018 at 11:03
  • 1
    Kindly check the updated answer just tested on one of my project and it worked fine! Nov 5, 2018 at 11:19
0

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;
    }
}
0
-1

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"; }
1
  • that solves the problem of listing the tables, but not the problem of outputting all the data from each table in turn into a HTML table. And also, code without any explanation is not actually very helpful to allow people to understand (rather than just blindly copy and paste)
    – ADyson
    Nov 5, 2018 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.