I was building up the prototype of my thesis, and wondered how to test if a table exists in database. so I googled.

[php]
function table_exists ( $table , $db ) { $tables = mysql<em>list</em>tables ( $db );
while ( list ( $temp ) = mysql<em>fetch</em>array ( $tables )) {
if ( $temp == $table ) { return TRUE ; }
} return FALSE ; }
[/php]

/ ** How to use it ** /
[php]
if ( table_exists ( test_table , my_database )) {
echo " Yes the table is there. ";
}
[/php]

Source: http://www.oxyscripts.com/itemdisplay.php?id=1003&code=yes

And a shorter, much more elegant method.

/ / here is a much more elegant method to check if a table exists ( no error generate )
[php]
if ( mysql_num_rows ( mysql_query (" SHOW TABLES LIKE ‘ ". $table ." ‘ ")))
{
/ /…
}
[/php]

Source: http://snippets.dzone.com/posts/show/3369

Bookmark and Share