Our blog

 

Check if MySQL Table Exists

Sometime you need a PHP script to check for the existence of a MySQL table. This function achieves that for you.

PHP:
  1. function db_table_exists($table){
  2.     $exists = false;
  3.     $tables_query = db_query("SHOW TABLES FROM " . MYSQL_DB);
  4.     while($t = mysql_fetch_assoc($tables_query)){
  5.         foreach($t as $k=>$v){
  6.             if($v == $table){
  7.                 $exists = true;
  8.                 break;
  9.             }
  10.         }
  11.     }
  12.     return $exists;
  13. }

More Reading:

 

Leave a Reply