print "table exists" if grep {"tablename" eq $_ } $dbh->tables; #### #!/usr/bin/perl -w use DBI; use strict; my $dbh = DBI->connect("DBI:mysql:mysql;host=localhost", "username","password", {RaiseError => 1}) or die "can't connect\n"; # # Checks for a table in a given database # sub table_exists { my ($dbh, $table) = @_; return grep {$table eq $_} $dbh->tables } my $mytable = "user"; if (table_exists($dbh,$mytable)) { print "table $mytable exists"; } else { print "table $mytable does not exist"; } $dbh->disconnect();