Nathan
Nathan

Reputation: 319

Deadman's switch: drop all tables from a database with php?

I'm working with a client who gives me some reason not to trust him (who hasn't been in that situation). I'm trying to create a "deadman's switch", where all tables in a database will be deleted on submission of a form (in a secret login protected directory).

What I don't know how to do is drop all tables from a database using PHP. I know how to drop columns and rows, and am sure individual tables can be dropped (but there are a lot of tables), but all tables within a database is beyond me.

Upvotes: 4

Views: 1480

Answers (5)

Charles Iams
Charles Iams

Reputation: 66

You can query "SHOW TABLES;" and then loop through each of those results and "DROP TABLE TABLENAME;"

But as someone already stated, if somebody else finds this and uses it. That would be a bad day. I wouldn't recommend doing this.

Upvotes: 1

Chris
Chris

Reputation: 3338

One option would be to use DROP DATABASE:

DROP DATABASE db_name;

http://dev.mysql.com/doc/refman/5.0/en/drop-database.html

This would simply destroy the database. Another option would be to select all tables within the database using the information_schema table:

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE table_schema = 'db_name'

http://dev.mysql.com/doc/refman/5.1/en/tables-table.html

You should check the permissions of your user on the production site before you trust this idea completely though. Some hosting companies might not give you access to information_schema or DROP DATABASE.

Upvotes: 1

Lior
Lior

Reputation: 6051

FIrst, You can read from information_schema, it stores all the tables names.
Second, You can use mysql_list_tables (it is deprecated) to get all tables of the db. Third, You can drop the whole db, instead of deleting each table.

Upvotes: 1

Pedro del Sol
Pedro del Sol

Reputation: 2841

drop database

DROP DATABASE <DATABASE_NAME>

Upvotes: 2

Mr. Alien
Mr. Alien

Reputation: 157314

Why don't you drop the entire database?

DROP DATABASE database_name

Sample Code

<?php
if(isset($_POST['delete_database'])) {
  //Code goes here
}
?>

<form method="post">
 <input type="submit" name="delete_database" value="Delete Entire Database" />
</form>

Upvotes: 5

Related Questions