What are some best practices for efficiently querying and displaying table and column information in PHP applications?

When working with databases in PHP applications, it's important to efficiently query and display table and column information. One best practice is to use SQL queries to retrieve metadata about tables and columns in the database. This information can then be displayed in a user-friendly format to provide insights into the database structure.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Query to get table information
$table_query = "SHOW TABLES";
$table_result = $conn->query($table_query);

// Display table names
if ($table_result->num_rows > 0) {
    while($row = $table_result->fetch_assoc()) {
        echo "Table: " . $row['Tables_in_database'] . "<br>";

        // Query to get column information
        $column_query = "SHOW COLUMNS FROM " . $row['Tables_in_database'];
        $column_result = $conn->query($column_query);

        // Display column names
        if ($column_result->num_rows > 0) {
            while($col = $column_result->fetch_assoc()) {
                echo "Column: " . $col['Field'] . "<br>";
            }
        }
    }
}

// Close the database connection
$conn->close();