How to get list of table names from database in mysql?

in mysql, PHP Code / 2 Comments

Hi friends,

I got the requirement to display the list of table names from the database using php code. I got the solution for it, once check the below code.

1) Connect to database:

mysql_connect(“dbserver”,”dbuser”,”dbpassword”);

2)Get the list of tables from table

$result = mysql_list_tables(“dbname”);

3) Count the total number of tables

$num_tables = mysql_num_rows($result);

3) Display the tables names

for($i=0;$i<$num_rows;$i++)

{

echo¬† “Table : <b>”.mysql_tablename($result,$i).”</b>”;

}

mysql_free_result($result);

In this way we can the total table names from the database.

With more than 9 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : php.anil@gmail.com

Discussion 2 Comments

  1. Gwapo2go

    mysql_list_tables is already depricated. You should use mysql_query() to issue SHOW TABLES command. Refer to the PHP Manual for additional details.

  2. Murali Krishna K

    Use $num_tables instead of $num_rows in for loop on the above mention code than it works fine.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>