How can the SHOW TABLES command be used to retrieve table names in MySQL queries in PHP?
To retrieve table names in MySQL queries in PHP, you can use the SHOW TABLES command. This command returns a result set of table names in the current database. You can then fetch these table names using PHP to display or use them in your application.
<?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);
}
// Execute SHOW TABLES command
$result = $conn->query("SHOW TABLES");
// Fetch table names
$tables = [];
while($row = $result->fetch_row()) {
$tables[] = $row[0];
}
// Display table names
foreach($tables as $table) {
echo $table . "<br>";
}
$conn->close();
?>