What potential pitfalls should be considered when trying to extract column names from a table in PHP?

When trying to extract column names from a table in PHP, potential pitfalls to consider include ensuring proper error handling in case the table or columns do not exist, handling any potential SQL injection vulnerabilities, and verifying the database connection is established before querying for column names.

<?php
// Establish a connection 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 get column names from a table
$table_name = "your_table_name";
$result = $conn->query("SHOW COLUMNS FROM $table_name");

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row['Field'] . "<br>";
    }
} else {
    echo "No columns found in the table.";
}

$conn->close();
?>