How can the SHOW TABLES LIKE query be used in PHP to check for the existence of a specific table in a database?
To check for the existence of a specific table in a database using the SHOW TABLES LIKE query in PHP, you can execute the query and then check if any rows are returned. If rows are returned, it means the table exists in the database.
<?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);
}
// Execute the SHOW TABLES LIKE query
$result = $conn->query("SHOW TABLES LIKE 'your_table_name'");
// Check if any rows are returned
if ($result->num_rows > 0) {
echo "Table exists in the database.";
} else {
echo "Table does not exist in the database.";
}
// Close the connection
$conn->close();
?>