如何用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)