38

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?

1

5 Answers 5

81

How to get 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

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

3. Using information schema

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:

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

and I think, information_schema would be better than 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>";
}
6
  • 4
    or SHOW TABLES IN DB_MANE
    – user557846
    Commented Nov 25, 2013 at 1:49
  • There is no reason not to use SHOW TABLES unless the active connection is making use of temporary tables.
    – user188654
    Commented Nov 25, 2013 at 1:54
  • 2
    @holodoc that's my personal opinion. title of SHOW TABLE's output is something like 'Tables_in_TBL_NAME'. so when executed in client program. So column name is changed. and people don't know about information_schema which has more useful information about DB
    – Jason Heo
    Commented Nov 25, 2013 at 2:02
  • 1
    Perfect. For some reason I thought that it'd just return an array of the actual tables, not rows.
    – Jadar
    Commented Nov 25, 2013 at 3:53
  • I like Solution #3 the best. The information_schema.tables table gives me a lot more info. Always nice to look into
    – peterb
    Commented Nov 27, 2017 at 4:10
8

Try this:

SHOW TABLES FROM nameOfDatabase;
2

SHOW TABLE_NAME is not valid. Try SHOW TABLES

TD

1
  • Whoops, meant to change that. I was testing different things and just copy-pasted from my code. It was SHOW TABLES
    – Jadar
    Commented Nov 25, 2013 at 3:27
0

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

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

-2
<?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 !!!!!!!!!!


2
  • 2
    This is just a code snippet. If it's an actual answer, then it should have some explanation as to how it works and why it's a better answer than the other ones.
    – Jadar
    Commented Mar 31, 2016 at 21:31
  • I found this useful as a quick snippet to experiment with. Thanks amit! Commented Apr 11, 2019 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.