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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20183553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:58:05  来源:igfitidea点击:

Show all tables inside a MySQL database using PHP?

phpmysqldatabase

提问by Jadar

I'm trying to show all the tables in my database. I've tried this:

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

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

but it only gives me one table name in a database I know has 2. What am I doing wrong?

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

回答by Jason Heo

How to get tables

如何获得桌子

1. SHOW TABLES

1. SHOW TABLES

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

2. SHOW TABLES IN db_name

2. SHOW TABLES IN db_name

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

3. Using information schema

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)

to OP

到操作

you have fetched just 1 row. fix like this:

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

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

and I think, information_schema would be better than SHOW TABLES

我认为,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>";
}

回答by kurt

Try this:

尝试这个:

SHOW TABLES FROM nameOfDatabase;

回答by Tim Duncklee

SHOW TABLE_NAME is not valid. Try SHOW TABLES

SHOW TABLE_NAME 无效。尝试显示表

TD

TD

回答by Hyman

SHOW TABLES only lists the non-TEMPORARY tables in a given database.

SHOW TABLES 仅列出给定数据库中的非临时表。

https://dev.mysql.com/doc/refman/5.0/en/show-tables.html

https://dev.mysql.com/doc/refman/5.0/en/show-tables.html

回答by amit

<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
?>
//Try This code is running perfectly !!!!!!!!!!