Tulisan kali ini saya ingin berbagi cara untuk menghapus semua tabel didalam sebuah database MySQL.
Langsung saja, berikut adalah scriptnya:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?PHP /* * @author : Ahmad Fauzi <[email protected]> * Project Name : drop-all-table * Generated : Nov 19, 2017 - 4:07:27 PM * Filename : droptable.php * Encoding : UTF-8 */ /* fill in your database name */ $database_name = "dbname"; /* connect to MySQL */ if (!$link = mysql_connect("dbhost", "dbuser", "dbpass")) { die("Could not connect: " . mysql_error()); } /* query all tables */ $sql = "SHOW TABLES FROM $database_name"; if ($result = mysql_query($sql)) { /* add table name to array */ while ($row = mysql_fetch_row($result)) { $found_tables[] = $row[0]; } } else { die("Error, could not list tables. MySQL Error: " . mysql_error()); } /* loop through and drop each table */ foreach ($found_tables as $table_name) { $sql = "DROP TABLE $database_name.$table_name"; if ($result = mysql_query($sql)) { echo "Success - table $table_name deleted."; } else { echo "Error deleting $table_name. MySQL Error: " . mysql_error() . ""; } } |
Selamat mencoba 🙂