How can you check if a table exists in a database using PHP?
To check if a table exists in a database using PHP, you can run a query to select information about the table from the information schema. If the query returns a result, then the table exists in the database.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$tableName = "your_table_name";
// Check if table exists
$result = $conn->query("SHOW TABLES LIKE '$tableName'");
if($result->num_rows > 0) {
echo "Table exists";
} else {
echo "Table does not exist";
}
$conn->close();
?>