从Mysql数据库中删除所有表,除了少数PHP与表和列相同

时间:2017-04-13 21:35:02

标签: php mysql

我想用PHP做以下...

DROP ALL TABLES
FROM DATABASE_NAME
WHERE TABLENAME != "NAME1"
  AND TABLENAME != "NAME2"
  AND TABLENAME != "NAME3"

我想对表和列做同样的事情......

DROP ALL COLUMNS
FROM TABLENAME
WHERE COLUMNNAME != "NAME1"
  AND COLUMNNAME != "NAME2"
  AND "COLUMNNAME" != "NAME3".

我需要使用php执行此操作并在我的Web浏览器中执行此脚本。

我试图找到一些可以在谷歌上运行但没有成功的东西。

希望你们能帮忙,这是我在stackoverflow上的第一个问题。

但Stackoverflow已经帮助了我很多次而且经常。 :)

1 个答案:

答案 0 :(得分:1)

问题解决了

 <?php
//Entfernt alle Tabellen die nicht Standartmäßig bestandteil des WBB sind.
//Aufruf über: clean_structure($db, $mysqldb);
function clean_structure($db, $dbname)
{
    $ergebnis = mysqli_query($db, "
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '$dbname'
AND TABLE_NAME NOT IN ('bb1_announcements', 'bb1_avatars', 'bb1_bbcode', 'bb1_boards', 'bb1_config', 'bb1_folders', 'bb1_groups', 'bb1_notify', 'bb1_object2board', 'bb1_object2user', 'bb1_pms', 'bb1_pmsend', 'bb1_poll', 'bb1_posts', 'bb1_ranks', 'bb1_smilies', 'bb1_style', 'bb1_threads', 'bb1_user_table', 'bb1_useronline', 'bb1_vote')
");
    while ($table = mysqli_fetch_array($ergebnis)) {
        mysqli_query($db, "DROP TABLE $table[0]");
    }
}

//Entfernt alle Spalten in einer Tabelle die nicht im Aufruf erwähnt werden
//Aufruf über: clean_tables($db, $mysqldb, "tablename", "'columntokeep1', 'columntokeep2', 'columntokeep3'");
function clean_tables($db, $dbname, $tabletoclean, $columnstokeep)
{
    $ergebnis = mysqli_query($db, "
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '$dbname'
AND TABLE_NAME = '$tabletoclean'
AND COLUMN_NAME NOT IN ($columnstokeep)
");
    while ($column = mysqli_fetch_array($ergebnis)) {
        mysqli_query($db, "ALTER TABLE $tabletoclean DROP $column[0]");
    }
}
?>