What are common methods to retrieve column names from a table in PHP when the table is empty?

When a table is empty in a database, traditional methods of retrieving column names, such as using SQL queries, may not work as expected. One common method to retrieve column names from an empty table in PHP is to use the `SHOW COLUMNS` query in MySQL. This query can be executed to fetch the column names even when the table is empty.

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

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to retrieve column names from an empty table
$sql = "SHOW COLUMNS FROM your_table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row['Field'] . "<br>";
    }
} else {
    echo "Table is empty or does not exist.";
}

// Close connection
$conn->close();
?>