我想显示数据库中的所有表格。我尝试过以下方法:
$sql = "SHOW TABLES";
$result = $conn->query($sql);
$tables = $result->fetch_assoc();
foreach($tables as $tmp)
{
echo "$tmp <br>";
}
但是它只在我知道有2个表的数据库中给出了一个表名。我做错了什么?
SHOW TABLES
mysql> USE test;
Database changed
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
| t3 |
+----------------+
3 rows in set (0.00 sec)
显示数据库db_name中的表
mysql> SHOW TABLES IN another_db;
+----------------------+
| Tables_in_another_db |
+----------------------+
| t3 |
| t4 |
| t5 |
+----------------------+
3 rows in set (0.00 sec)
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)
你只获取了一行数据,请按以下方式进行修复:
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>";
}
试试这个:
SHOW TABLES FROM nameOfDatabase;
SHOW TABLE_NAME不是有效的命令,请尝试使用SHOW TABLES。
TD
SHOW TABLES 只列出给定数据库中非 TEMPORARY 表。
<?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 !!!!!!!!!!
SHOW TABLES IN DB_MANE
- user557846SHOW TABLES
。 - user188654SHOW TABLE
命令的输出标题类似于'Tables_in_TBL_NAME'
。因此,在客户端程序中执行时,列名会发生变化。而且人们不知道information_schema
,它包含有关数据库更有用的信息。 - Jason Heoinformation_schema.tables
表给了我更多的信息。总是很好去了解。 - peterb$tables
更改为$table
可能有助于澄清每行包含关于一个表的信息。我知道名称只是从问题中复制过来的,但最好纠正 OP 对返回内容的困惑。或者使用传统名称,如$row
。 - ToolmakerSteve