在我們對網站進行更新維護時,可能對刪除相同資料庫表的操作比較頭疼,今天我們就向大傢具體講解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;
- }
- /*截取某個特定字元前的所有字元函數
- *$str 為待截取字串
- *$flag 特定字元如“_”
- */
- function mysubstr($str,$flag){
- $pos=strpos($str,$flag);
- return substr($str,0,$pos);
- }
- ?>
- $dbname="shujukuming";//資料庫名
- $tableflag="xx";//要刪除的表的首碼,有這個首碼的表才刪除,標誌出是哪一個使用者
- deldata($dbname,$tableflag);
- //$test=mysubstr("cdb_account_log","_");
- //echo $test;
- ?>
PHP刪除資料庫表更改之處在:
1.開頭處
- function deldata($dbname,$tableflag){
- $db_host = 'localhost';
- $db_port = '3306';
- $db_user = 'user';
- $db_pass = 'password';
改為自己的資料庫地址,帳號和密碼即可
2.結尾處
- $dbname="shujukuming";//資料庫名
- $tableflag="xx";//要刪除的表的首碼,有這個首碼的表才刪除,標誌出是哪一個使用者
- deldata($dbname,$tableflag);
- //$test=mysubstr("cdb_account_log","_");
- //echo $test;
- ?>
-
改為自己的資料庫名和想刪掉的表首碼就可完成PHP刪除資料庫表。
可以複製上面的代碼儲存為.php,再上傳到空間目錄開啟
推薦可以下載此.php檔案http://flysh.toypark.in//up/1255275586.umd下載後改尾碼為php,名字自取再上傳開啟即可。
http://www.bkjia.com/PHPjc/446309.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446309.htmlTechArticle在我們對網站進行更新維護時,可能對刪除相同資料庫表的操作比較頭疼,今天我們就向大傢具體講解 PHP刪除資料庫表具體實現代碼如下...