How to check whether table exists in database
<?
function check_table($host, $user, $pass, $db, $tbl)
{
$tables = array();
$link = @mysql_connect($host, $user, $pass);
@mysql_select_db($db);
$q = @mysql_query(”SHOW TABLES”);
while ($r = @mysql_fetch_array($q)) { $tables[] = $r[0]; }
@mysql_free_result($q);
@mysql_close($link);
if (in_array($tbl, $tables)) { return TRUE; }
else { return FALSE; }
}
if (check_table(’localhost’, ‘root’, ‘’, ‘db1′, ‘table1′)) {
echo ‘Table exists in database’;
} else {
echo ‘Table doesn’t exists’;
}
?>
The above specified example is used to check whether the table “table1″ is present in database “db1″. In check_table() function, we are passing host name, user name, password, database name and password. If table exists, check_table() function returns true, otherwise false.
