query($sql); $tables = $result->fetc-6ren">
gpt4 book ai didi

php - 使用 PHP 显示 MySQL 数据库中的所有表?

转载 作者:IT老高 更新时间:2023-10-28 13:00:16 26 4
gpt4 key购买 nike

我正在尝试显示我的数据库中的所有表。我试过这个:

$sql = "SHOW TABLES";
$result = $conn->query($sql);
$tables = $result->fetch_assoc();
foreach($tables as $tmp)
{
echo "$tmp <br>";
}

但它只给了我一个我知道有 2 的数据库中的表名。我做错了什么?

最佳答案

如何获取表格

1。 显示表格

mysql> USE test;
Database changed
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
| t3 |
+----------------+
3 rows in set (0.00 sec)

2。 在 db_name 中显示表

mysql> SHOW TABLES IN another_db;
+----------------------+
| Tables_in_another_db |
+----------------------+
| t3 |
| t4 |
| t5 |
+----------------------+
3 rows in set (0.00 sec)

3。使用信息架构

mysql> SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'another_db';
+------------+
| TABLE_NAME |
+------------+
| t3 |
| t4 |
| t5 |
+------------+
3 rows in set (0.02 sec)

到 OP

您只提取了 1 行。像这样修复:

while ( $tables = $result->fetch_array())
{
echo $tmp[0]."<br>";
}

我认为,information_schema 会比 SHOW TABLES

SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'

while ( $tables = $result->fetch_assoc())
{
echo $tables['TABLE_NAME']."<br>";
}

关于php - 使用 PHP 显示 MySQL 数据库中的所有表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com