Check if MySQL Table Exists

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

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

function db_table_exists($table){
	$exists = false;
	$tables_query = db_query("SHOW TABLES FROM " . MYSQL_DB);
	while($t = mysql_fetch_assoc($tables_query)){
		foreach($t as $k=>$v){
			if($v == $table){
				$exists = true;
				break;
			}
		}
	}
	return $exists;
}

Tags: mysqlphptableexists