How show all tables in mysql using php?

The square brackets in your code are used in the mysql documentation to indicate groups of optional parameters. They should not be in the actual query.

The only command you actually need is:

show tables;

If you want tables from a specific database, let's say the database "books", then it would be

show tables from books;

You only need the LIKE part if you want to find tables whose names match a certain pattern. e.g.,

show tables from books like '%book%';

would show you the names of tables that have "book" somewhere in the name.

Furthermore, just running the "show tables" query will not produce any output that you can see. SQL answers the query and then passes it to PHP, but you need to tell PHP to echo it to the page.

Since it sounds like you're very new to SQL, I'd recommend running the mysql client from the command line [or using phpmyadmin, if it's installed on your system]. That way you can see the results of various queries without having to go through PHP's functions for sending queries and receiving results.

If you have to use PHP, here's a very simple demonstration. Try this code after connecting to your database:

$result = mysql_query["show tables"]; // run the query and assign the result to $result
while[$table = mysql_fetch_array[$result]] { // go through each row that was returned in $result
    echo[$table[0] . "
"]; // print the table that was returned on that row. }

[PHP 4, PHP 5]

mysql_list_tablesList tables in a MySQL database

Warning

This function was deprecated in PHP 4.3.0, and it and the entire original MySQL extension was removed in PHP 7.0.0. Instead, use either the actively developed MySQLi or PDO_MySQL extensions. See also the MySQL: choosing an API guide. Alternatives to this function include:

  • SQL Query: SHOW TABLES FROM dbname

Description

mysql_list_tables[string $database, resource $link_identifier = NULL]: resource|false

This function is deprecated. It is preferable to use mysql_query[] to issue an SQL SHOW TABLES [FROM db_name] [LIKE 'pattern'] statement instead.

Parameters

database

The name of the database

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect[] is assumed. If no such link is found, it will try to create one as if mysql_connect[] had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Return Values

A result pointer resource on success or false on failure.

Use the mysql_tablename[] function to traverse this result pointer, or any function for result tables, such as mysql_fetch_array[].

Changelog

VersionDescription
4.3.7 This function became deprecated.

Examples

Example #1 mysql_list_tables[] alternative example

Notes

Note:

For backward compatibility, the following deprecated alias may be used: mysql_listtables[]

See Also

  • mysql_list_dbs[] - List databases available on a MySQL server
  • mysql_tablename[] - Get table name of field

daveheslop [dave heslop]

17 years ago

Worth noting for beginners: using a row count to test for the existence of a table only works if the table actually contains data, otherwise the test will return false even if the table exists.

NewToPHP_Guy at Victoria dot NOSPAM dot com

19 years ago

The example by PHP-Guy to determine if a table exists is interesting and useful [thanx], except for one tiny detail.  The function 'mysql_list_tables[]' returns table names in lower case even when tables are created with mixed case.  To get around this problem, add the 'strtolower[]' function in the last line as follows:

return[in_array[strtolower[$tableName], $tables]];

bimal at sanjaal dot com

9 years ago

A better alternative to mysql_list_tables[] would be the following mysql_tables[] function.

coffee at hayekheaven dot net

20 years ago

Even though php guy's solution is probably the fastest here's another one just for the heck of it...
I use this function to check whether a table exists. If not it's created.

mysql_connect["server","usr","pwd"]
    or die["Couldn't connect!"];
mysql_select_db["mydb"];

$tbl_exists = mysql_query["DESCRIBE sometable"];
if [!$tbl_exists] {
mysql_query["CREATE TABLE sometable [id int[4] not null primary key,
somevalue varchar[50] not null]"];
}

kroczu at interia dot pl

15 years ago

wbphfox at xs4all dot nl

19 years ago

Here is a way to show al the tables and have the function to drop them...

mail at thomas-hoerner dot de

19 years ago

You can also use mysql_fetch_object if you consider a specialty: The name of the object-var is

Tables_in_xxxxx

where xxxxx is the name of the database.

i.e. use

$result = mysql_list_tables[$dbname];
$varname="Tables_in_".$dbname;
while [$row = mysql_fetch_object[$result]] {
   echo $row->$varname;
};

How show all tables in SQL PHP?

$result = mysql_query["show tables"]; // run the query and assign the result to $result while[$table = mysql_fetch_array[$result]] { // go through each row that was returned in $result echo[$table[0] . "
"]; // print the table that was returned on that row. } Show activity on this post. Show activity on this post.

How can I see all tables in MySQL?

MySQL Show/List Tables.
Step 1: Open the MySQL Command Line Client that appeared with a mysql> prompt. ... .
Step 2: Next, choose the specific database by using the command below:.
Step 3: Finally, execute the SHOW TABLES command..
Output:.
Syntax..

How display all data in MySQL using PHP?

The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.

How can I see all tables in a database?

Then issue one of the following SQL statement:.
Show all tables owned by the current user: SELECT table_name FROM user_tables;.
Show all tables in the current database: SELECT table_name FROM dba_tables;.
Show all tables that are accessible by the current user:.

Chủ Đề