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

PHP batch deletes tables with specified prefixes under the database. Take prefix_ as an example, batch delete prefix_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:20:06806browse

php batch deletes tables with specified prefixes under the database. Take prefix_ as an example to batch delete prefix

How to use php to batch delete all tables with prefix_ in the database.

Example, delete tables with the prefix "prefix_" uniformly.

<&#63;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>"; 
} 
} 
} 
&#63;>

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.

Mysql database batch deletes data tables with the same prefix

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

mysql finds all tables with a specific prefix in a database

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)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868485.htmlTechArticlephp batch deletes tables with specified prefixes under the database. Take prefix_ as an example to batch delete prefix. How to batch delete databases with php All tables with prefix_ are listed below. For example, delete the prefix "...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn