What potential pitfalls should be considered when checking for the existence of a table in PHP?

When checking for the existence of a table in PHP, it is important to consider potential pitfalls such as SQL injection vulnerabilities and error handling. To mitigate these risks, it is recommended to use prepared statements and proper error handling techniques when querying the database for table existence.

<?php
// Establish a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

// Check for table existence
$table_name = "your_table_name";
$sql = "SHOW TABLES LIKE '$table_name'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "Table exists!";
} else {
    echo "Table does not exist!";
}

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