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.