In PHP, you can use a MySQL database. SHOW TABLES The command retrieves the names of all data tables in the current database and then uses them. DROP TABLE Command to batch delete data tables with a specified prefix.
Here is a simple PHP function for batch deletion of database tables with a specified prefix:
function deleteTables($prefix) {
// 设置 MySQL 数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// 创建 MySQL 数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取所有数据表名
$result = $conn->query("SHOW TABLES");
// 遍历数据表名
while ($row = $result->fetch_assoc()) {
// 获取当前数据表名
$table_name = $row['Tables_in_' . $dbname];
// 如果数据表名以指定前缀开头,那么删除该数据表
if (strpos($table_name, $prefix) === 0) {
$conn->query("DROP TABLE $table_name");
}
}
// 关闭 MySQL 数据库连接
$conn->close();
}In the above code,deleteTables The function accepts a single parameter. $prefixThe prefix of the data table to be deleted. This function first establishes a MySQL database connection, then uses it. SHOW TABLES The command retrieves all table names from the current database and iterates over these table names. For each table name, the function checks whether it begins with the specified prefix; if so, it is used. DROP TABLE Issue the command to delete this data table. Finally, the function closes the MySQL database connection.
Please note that deleting a data table is a destructive operation; therefore, before running this function, be sure to back up your database and carefully verify the name of the data table you intend to delete to avoid deleting any table that should not be removed.