如何用php批量删除数据库下所有前缀为prefix_的表。
例子,统一删除前缀为“prefix_”的表。
<?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>";
}
}
}
?>操作实例:
新建一个php文件,保存为deletedata.php。
比如,要删除www.jb51.net或者http://www.bkjia.com/的后台数据,操作两个步骤就ok:
1. 首先将这个保存好的deletedata.php文件上传至你的网站根目录;
2. 直接在地址栏输入:www.jb51.net/deletedata.php或者http://www.bkjia.com/deletedata.php执行这个删除脚本即中。
该脚本会在浏览器中显示出所有表删除成功的信息。
先建立一个删除脚本
运行下语句
select 'drop table '+name from sysobjects where type='U' and name like 'B%'
会出来查询结果,将查询结果复制出来
然后在数据库123中运行复制出来的查询结果
php 不会。
mysql 里面, 查询 表的信息, 我倒是会的。
是从 information_schema.tables 这里查询的。
下面是一个例子:
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 | BASE TABLE | InnoDB |
| test_create_tab | BASE TABLE | InnoDB |
| sale_report | BASE TABLE | InnoDB |
| log_table | BASE TABLE | InnoDB |
+--------------------+------------+--------+
15 rows in set (0.02 sec)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号