IT & Programming

Check if mysql table exists

  • How to check if mysql table exists.
  • Check a mysql table with php.
  • Search if mysql table exists.
  • Finding a mysql table with php.

This scripts checks if a mysql table exists or not. Function saves all mysql table names into $tables array and then with PHP's in_array function we can check if specific table name exists inside $tables array or not.

Script

$tables = array();

$q = mysql_query("SHOW TABLES");

while ($row = mysql_fetch_array($q)){
    $tables[] = $row[0];
} 

$table_to_find = 'products';

if (!(in_array($table_to_find, $tables))){
    
	echo "Table does not exist";
}else{

	echo "Table exists";
}

Comments

  1. Yreenas November 11, 2012

    It helps a lot! thanksaaa

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *