Home >Backend Development >PHP Tutorial >PHP batch deletes tables with specified prefixes under the database. Take prefix_ as an example, batch delete prefix_PHP tutorial
How to use php to batch delete all tables with prefix_ in the database.
Example, delete tables with the prefix "prefix_" uniformly.
<?php //设置数据库连接信息。数据库服务器地址,数据库用户名,数据密码 mysql_connect('数据库主机','数据库用户名','数据库密码'); //设置查询的数据库名称 mysql_select_db('数据库名'); $rs=mysql_query('show tables'); while($arr=mysql_fetch_array($rs)) { //设置要批量删除的数据库表前缀,如:prefix_ $TF=strpos($arr[0],'prefix_'); if($TF===0){ $FT=mysql_query("drop table $arr[0]"); if($FT){ echo "$arr[0] 删除成功!<br>"; } } } ?>
Operation example:
Create a new php file and save it as deletedata.php.
For example, to delete the background data of www.jb51.net or http://www.bkjia.com/, just follow two steps:
1. First upload the saved deletedata.php file to the root directory of your website;
2. Directly enter: www.jb51.net/deletedata.php or http://www.bkjia.com/deletedata.php in the address bar to execute the delete script.
This script will display the successful deletion information of all tables in the browser.
First create a delete script
Run the statement
select 'drop table '+name from sysobjects where type='U' and name like 'B%'
will come out Query the results, copy the query results
and then run the copied query results in database 123
php doesn’t.
I know how to query table information in mysql.
is queried from information_schema.tables.
The following is an example:
mysql> SELECT table_name, table_type, engine
-> FROM information_schema.tables
-> WHERE table_schema = 'test'
-> ORDER BY table_name DESC;
-> //
+--------------------+--------- ---+--------+
| table_name | table_type | engine |
+--------------------+-- ----------+--------+
| v_sale_report_x | VIEW | NULL |
| v_sale_report | VIEW | NULL |
| union_tab_2 | BASE TABLE | InnoDB |
| union_tab_1 | BASE TABLE | InnoDB |
| test_trigger_table | BASE TABLE | InnoDB |
| test_tab2 | BASE TABLE | InnoDB |
| test_tab | BASE TABLE | InnoDB |
| test_main | BASE TABLE | InnoDB |
| test_dysql | BASE TABLE | InnoDB |
| test_create_tab4 | BASE TABLE | InnoDB |
| test_create_tab2 | BASE TABLE | InnoDB |
| test_create_tab1 |
| test_create_tab | BASE TABLE | InnoDB |
| sale_report | BASE TABLE | InnoDB |
| log_table | BASE TABLE | InnoDB |
+------------- -------+----------------+--------+
15 rows in set (0.02 sec)