Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL table or database exists

Status
Not open for further replies.

JoeNg

Programmer
Apr 17, 2001
174
US
I like to check if a MySQL database or table exists. I searched on-line and cpan.org. I can't seem to find the right command. Any ideas?

Thanks

Joe

 
maybe there is a more elegant way to do this, but for starters, you can try this:

Code:
$sth = $dbh->prepare("SHOW TABLES"); #or SHOW DATABASES
$sth->execute;
while ( my $row = $sth->fetchrow_arrayref ) { die 'haha' if $$row[0] eq 'myTable'; }

assuming ofcourse you have the permission to run these queries.

HTH,
san

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
133tcamel,

Thanks for the info.

I was hoping it has a simple function like table_exists.

Thanks

Joe
 
If MySQL supports the INFORMATION_SCHEMA.* tables, you could run a query against them instead - at least you could put a WHERE clause on the SELECT for your table...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top