dropping multiple tables

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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

dropping multiple tables

Post by yaron »

Hello all,

My wish is a simpel one (i hope).
I need a mysql query to drop multiple tables from my database.
I have a prefix of the name of several tables and I wish to drop all tables that begins with that name.

is this possible?
if not how can I get all tables that begins with that prefix and then delete them one by one?

Thx all
jigaruu
Forum Newbie
Posts: 21
Joined: Wed Feb 18, 2004 11:46 pm
Contact:

Post by jigaruu »

for the three tables 'table1' 'table2' and 'table3' run the following command

DROP TABLE `table1`, `table2`,`table3`;

thats-it.
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

hmm... a bit too easy....
As I said I want to drop tables with the same prefix.
i.e. if I have 3 tables t1,t2 and s1
and I want to drop all the tables that begins with a t (assuming I don't know how many there are and what comes after 't')
can it be done?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could first "SHOW TABLES LIKE 't%'" then loop over that result set deleting each one.
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

thanks.
just what I was looking for :)
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

Just a quick question...
When I'm looping by using mysql_fetch_array how do I actually get the table name?

$array['table']????
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

while($row = mysql_fetch_array($result)){
echo $row[0]; //this is the table name
}
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

Thanks
Problem solved!!!
Post Reply