How can PHP code be structured to properly iterate through and display the results of a "SHOW TABLES" query in MySQL?

To properly iterate through and display the results of a "SHOW TABLES" query in MySQL using PHP, you can use a while loop to fetch each row of the result set and display the table names. This allows you to dynamically display the tables without hardcoding each one.

<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Perform "SHOW TABLES" query
$result = mysqli_query($connection, "SHOW TABLES");

// Iterate through results and display table names
echo "Tables in database:<br>";
while ($row = mysqli_fetch_row($result)) {
    echo $row[0] . "<br>";
}

// Close connection
mysqli_close($connection);
?>