Checking table existence before DROPping it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Checking table existence before DROPping it

Post by mjseaden »

Dear All,

How do I check for the existence of a table in a MySQL database, and if so, delete it?

Many thanks

Mark
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can use mysql_list_tables() and then see if its in the results, if it is then delete it:

Code: Select all

DROP TABLE `the_table_i_want_to_delete`
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi Kettle,

Is there any form of 'IF EXIST()' check I can use to determine a table's existence? Otherwise will it not produce an error?

Many thanks

Mark
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

DROP TABLE Syntax wrote:DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]
Post Reply