【发布时间】:2013-12-09 14:48:48
【问题描述】:
我正在尝试显示我的数据库中的所有表。我试过这个:
$sql = "SHOW TABLES";
$result = $conn->query($sql);
$tables = $result->fetch_assoc();
foreach($tables as $tmp)
{
echo "$tmp <br>";
}
但它只给了我一个我知道有 2 的数据库中的表名。我做错了什么?
【问题讨论】:
我正在尝试显示我的数据库中的所有表。我试过这个:
$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)
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)
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)
您只提取了 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>";
}
【讨论】:
SHOW TABLES IN DB_MANE
SHOW TABLES,除非活动连接正在使用临时表。
SHOW TABLE 的输出标题类似于 'Tables_in_TBL_NAME'。所以在客户端程序中执行时。所以列名被改变了。人们不知道 information_schema 有更多关于 DB 的有用信息
information_schema.tables 表为我提供了更多信息。总是很高兴看到
试试这个:
SHOW TABLES FROM nameOfDatabase;
【讨论】:
SHOW TABLE_NAME 无效。尝试显示表格
TD
【讨论】:
SHOW TABLES 仅列出给定数据库中的非临时表。
【讨论】:
<?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 !!!!!!!!!!
【讨论】: