


php中批量刪除Mysql中相同前綴的數(shù)據(jù)表的方法
用php開發(fā)程序時,批量刪除相同前綴的數(shù)據(jù)表的兩種方法如下:
方法一:
復制代碼 代碼如下:
<?php mysql_connect('','',''); mysql_select_db(''); $rs=mysql_query('show tables'); while($arr=mysql_fetch_array($rs)){ $TF=strpos($arr[0],'class_'); if($TF===0){ $FT=mysql_query("drop table $arr[0]"); if($FT){ echo "$arr[0] 刪除成功!<br>"; } } } ?>
方法二:
這個是以xx_為前綴的示范,大家可以自己更改為想刪除的表前綴。
復制代碼 代碼如下:
<?php function deldata($dbname,$tableflag){ $db_host = 'localhost'; $db_port = '3306'; $db_user = 'user'; $db_pass = 'password'; $connect =mysql_connect($db_host,$db_user,$db_pass); mysql_select_db($dbname); $result = mysql_query("show table status from $dbname",$connect); $data=mysql_fetch_array($result); while($data=mysql_fetch_array($result)) { $table=mysubstr($data[Name],"_"); if($table==$tableflag){ //測試之用 /*echo $data[Name]; echo " "; echo $table; echo " ";*/ mysql_query("drop table $data[Name]"); } } return true; } /*截取某個特定字符前的所有字符函數(shù) *$str 為待截取字符串 *$flag 特定字符如"_" */ function mysubstr($str,$flag){ $pos=strpos($str,$flag); return substr($str,0,$pos); } ?>
更改之處在:
1.開頭處
<?php function deldata($dbname,$tableflag){ $db_host = 'localhost'; $db_port = '3306'; $db_user = 'user'; $db_pass = 'password'; 改為自己的數(shù)據(jù)庫地址,賬號和密碼即可。
2.結(jié)尾處
改為自己的數(shù)據(jù)庫名和想刪掉的表前綴。
可以復制上面的代碼保存為.php,再上傳到空間目錄打開。這樣我們就能完成數(shù)據(jù)表的批量刪除了。
關(guān)鍵字:php、批量刪除、Mysql、數(shù)據(jù)表
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認iptable規(guī)則詳解