Rate this script:  I Love it  /   I Hate it

Test if MySQL table exists with PHP.


Code


<?
function table_exists ($table, $db) {
        $tables = mysql_list_tables ($db);
        while (list ($temp) = mysql_fetch_array ($tables)) {
                if ($temp == $table) {
                        return TRUE;
                }
        }
        return FALSE;
}

/** How to use it **/
if (table_exists(test_table, my_database)) {
        echo"Yes the table is there.";
}
?>

<?
// here is a much more elegant method to check if a table exists ( no error generate)

if( mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'")))
{
 //...
}

?>
 

 

 
Test if MySQL table exists with PHP. scripts | Test if MySQL table exists with PHP. snippet | Test if MySQL table exists with PHP. example | Test if MySQL table exists with PHP. tutorial | Test if MySQL table exists with PHP. code