DANNY CHANG NEW YORK
Test if MySQL table exists with PHP
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]
| Print article | This entry was posted by admin on November 26, 2009 at 11:15 am, and is filed under web. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Thanx, the elegant way to check if a database table exists saves my life!
about 1 year ago
I have implemented the same in php using inbuild php functions . Check it out in my blog – http://blog.sriunplugged.com/php/check-table-exists-mysql-php/