954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to show the table names from a database?

hi everyone. i want to list and show the table names in a database in a php page. how to do that? in phpmyadmin 'show tables' works in query window. bt showing it through a php script i think i would have to SELECT?

urbangeek
Light Poster
41 posts since Mar 2010
Reputation Points: 13
Solved Threads: 1
 

SHOW TABLES is the actual query.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

thanks for replying pritaeas...bt how can i show the list in a page through php?

urbangeek
Light Poster
41 posts since Mar 2010
Reputation Points: 13
Solved Threads: 1
 

Hi,

I just borrowed this from someone...please see the script's credit on top. This might be more than what you need..if it just table names that you want, then just remove the bottom part of the script.

<?php
/****************
* File: displaytables.php
* Date: 1.13.2009
* Author: design1online.com
* Purpose: display all table structure for a specific database
****************/

//connection variables

$host = "localhost";
$database = "database";
$user = "user";
$pass = "password";

//connection to the database
mysql_connect($host, $user, $pass)
or die ('cannot connect to the database:' . mysql_error());

//select the database
mysql_select_db($database)
or die ('cannot select database:' . mysql_error());

//loop to show all the tables and fields
$loop = mysql_query("SHOW tables FROM $database")
or die ('cannot select tables');

while($row = mysql_fetch_array($loop))
{

echo "
<table cellpadding=2 cellspacing=2 border=0 width=75%>
<tr bgcolor=#666666>
<td colspan=5><center><b><font color=#FFFFFF>” . $row[0] . “</font></center></td>
</tr>
<tr>
<td>Field</td><td>Type</td><td>Key</td><td>Default</td><td>Extra</td>
</tr>";

$i = 0;

$loop2 = mysql_query("SHOW columns FROM " . $row[0])
or die ('cannot select table fields');

while ($row2 = mysql_fetch_array($loop2))
{
echo "<tr ";
if ($i % 2 == 0)
echo "bgcolor=#CCCCCC";
echo "><td>". $row2[0] . "</td><td>". $row2[1] . "</td><td>" . $row2[2] . "</td><td>" . $row2[3] . "</td><td>" . $row2[4] . "</td></tr>";
$i++;
}
echo "</table><br/><br/>";

}
?>
veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

thanks buddy....this will do...

urbangeek
Light Poster
41 posts since Mar 2010
Reputation Points: 13
Solved Threads: 1
 

you are very much welcome :).

veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: