How can PHP be used to output all tables in a MySQL database?
To output all tables in a MySQL database using PHP, you can query the information_schema database to retrieve a list of all tables. You can then loop through the result set and display the table names.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to get all tables in the database
$sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '$dbname'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output all table names
while($row = $result->fetch_assoc()) {
echo "Table name: " . $row["table_name"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- What are the common mistakes to avoid when using the array_merge function in PHP for merging arrays?
- What are the best practices for handling URL parameters in PHP scripts to avoid conflicts and errors?
- How can PHP developers effectively extract specific data from text files with varying structures, like in the provided example?