How to check whether table exists in database

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.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.