MySQL: How to View All Table Names

To view the names of all tables in a MySQL database, you can use the following SQL query:

SHOW TABLES;

This will return a result set that contains the names of all tables in the database. Each table’s name will be displayed as a row in the result set.

Below is a code snippet showing an example query and its output.

// 连接到 MySQL 数据库
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database_name';

$conn = mysqli_connect($host, $username, $password, $database);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}

// 执行查询
$sql = "SHOW TABLES";
$result = mysqli_query($conn, $sql);

// 处理结果集
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['Tables_in_database_name'] . "<br>";
    }
} else {
    echo "没有找到表";
}

// 关闭数据库连接
mysqli_close($conn);

在上面的示例中,我们首先连接到 MySQL 数据库,然后执行 SHOW TABLES 查询。如果查询返回了结果,则使用循环遍历结果集并输出每个表的名称。如果查询未返回结果,则输出提示消息表示未找到任何表。最后,关闭数据库连接。

Please make sure to replace the username, password, and database_name in the example with your own database credentials and database name.

bannerAds