

Nov 14th, 2008
Maruf scribbled this post.
Maruf scribbled this post.
Someone asked me the other day how to display the existing tables in their database with a PHP script. The query itself is pretty simple, but people seem to get confused about the PHP side of it. Then again, once you look at the code, you might be surprised how easy it really is…
Printing tables with PHP
View Code PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? $showtablequery = " SHOW TABLES FROM [database] "; $showtablequery_result = mysql_query($showtablequery); while($showtablerow = mysql_fetch_array($showtablequery_result)) { echo $showtablerow[0]."<br />"; } ?> |
That query will show ALL tables, but you can use a LIKE function in the query as well, to get particular tables only.
Printing particular tables with PHP
View Code PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? $showtablequery = " SHOW TABLES FROM [database] LIKE '%brightcherry%' "; $showtablequery_result = mysql_query($showtablequery); while($showtablerow = mysql_fetch_array($showtablequery_result)) { echo $showtablerow[0]."<br />"; } ?> |
That query will pull out all tables with the word ‘brightcherry’ in.
Filed away: MySQL & PHP










comments
Hey man might sound like a silly question im new to php and all – but why does the array only print out properly when you specify the key as 0?
echo $showtablerow[0]
does the key increment in the while loop automatically?
Hey Ben,
The 0 doesn’t increment. 0 is the index- it’s calling the first element in the array, which is the tablename.
You could rename the variable to something like this:
$tablename = $showtablerow[0];
echo $tablename;
Now $tablename will print the tablenames.