Hongmu Notes
Home Language Notes PHP batch deletes data tables with specified prefix
Language Notes PHP PHP and mysql

PHP batch deletes data tables with specified prefix

PHP batch deletes data tables with specified prefix

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.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 181
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 146

Recommended reading

Household Care and Postpartum Care Service Website Template – 0939

Household Care and Postpartum Care Service Website Template – 0939 Practical Collection Yiyou template

This EyouCMS template is ideal for the domestic services, postpartum care, and nanny service industries. Its warm yet professional design is perfect for showcasing domestic services, postpartum care and nanny services, cleaning services, as well as staff profiles. It helps domestic service providers attract household clients online. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Eyou...
👁 51
(Adaptive Mobile Version) Valve Adjustment Website Template – Pneumatic Ball Valve Website Source Code Download – 1102

(Adaptive Mobile Version) Valve Adjustment Website Template – Pneumatic Ball Valve Website Source Code Download – 1102 Practical Collection pbootcms Template

A PbootCMS website template for regulating valves and pneumatic ball valves, compatible with both PC and WAP devices. The professional industrial design is ideal for showcasing regulating valve and ball valve products as well as their technical specifications. It enables valve manufacturing companies to showcase their products online and attract customers from the petrochemical and industrial sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 56
(With mobile version) Green Eco-Agricultural Enterprise Website PBootCMS Template – Agricultural Planting Website Source Code Download – 0135

(With mobile version) Green Eco-Agricultural Enterprise Website PBootCMS Template – Agricultural Planting Website Source Code Download – 0135 Practical Collection pbootcms Template

A PbootCMS template designed for green eco-agricultural enterprises, featuring a dedicated mobile version, ideal for agricultural farming and agricultural product businesses – compatible with both PC and mobile devices. Its natural and fresh design style effectively showcases eco-farming bases, agricultural products, and farming technologies. This template helps agricultural enterprises share their brand stories and product quality online, while expanding their sales channels. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: ad...
👁 50
Home Services & Cleaning Website Template 0940

Home Services & Cleaning Website Template 0940 Practical Collection Yiyou template

An EyouCMS website template designed for the domestic services and cleaning industry. Its fresh and professional design allows you to showcase cleaning services, domestic service offerings, service personnel, and customer reviews. This template helps domestic service companies build a strong online brand presence and attract residential clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 48
(PC+WAP) Floor Slab Website Template – Download Construction Project Website Source Code – 1103

(PC+WAP) Floor Slab Website Template – Download Construction Project Website Source Code – 1103 Practical Collection pbootcms Template

A building slab panel and construction project PbootCMS website template, compatible with both PC and WAP devices. The professional architectural design style is ideal for showcasing building slab panel products, steel structure projects, and construction case studies. It helps building material companies showcase their products online and attract construction developers and project clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 32
(Independent Mobile Version) pbootCMS Financial Landing Page Website Template – Download Single-Page Financial Website Source Code – 0136

(Independent Mobile Version) pbootCMS Financial Landing Page Website Template – Download Single-Page Financial Website Source Code – 0136 Practical Collection pbootcms Template

This PbootCMS template is a standalone mobile-friendly landing page specifically designed for financial services – such as P2P lending and wealth management – for marketing and promotional purposes. Its design features a clear, marketing-driven approach with a clean and impactful layout that effectively highlights the advantages of financial products and investment returns. It serves as an efficient tool for financial platforms to run mobile advertising campaigns and acquire new customers. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin
👁 49