深入探討PHP刪除資料庫表的具體方式_PHP教程

來源:互聯網
上載者:User
在我們對網站進行更新維護時,可能對刪除相同資料庫表的操作比較頭疼,今天我們就向大傢具體講解PHP刪除資料庫表具體實現代碼如下

  1. function deldata($dbname,$tableflag){
  2. $db_host = 'localhost';
  3. $db_port = '3306';
  4. $db_user = 'user';
  5. $db_pass = 'password';
  6. $connect =mysql_connect($db_host,$db_user,$db_pass);
  7. mysql_select_db($dbname);
  8. $result = mysql_query("show table status from $dbname",$connect);
  9. $data=mysql_fetch_array($result);
  10. while($data=mysql_fetch_array($result)) {
  11. $table=mysubstr($data[Name],"_");
  12. if($table==$tableflag){
  13. //測試之用
  14. /*echo $data[Name];
  15. echo "
  16. ";
  17. echo $table;
  18. echo "
  19. ";*/
  20. mysql_query("drop table $data[Name]");
  21. }
  22. }
  23. return true;
  24. }
  25. /*截取某個特定字元前的所有字元函數
  26. *$str 為待截取字串
  27. *$flag 特定字元如“_”
  28. */
  29. function mysubstr($str,$flag){
  30. $pos=strpos($str,$flag);
  31. return substr($str,0,$pos);
  32. }
  33. ?>

  1. $dbname="shujukuming";//資料庫名
  2. $tableflag="xx";//要刪除的表的首碼,有這個首碼的表才刪除,標誌出是哪一個使用者
  3. deldata($dbname,$tableflag);
  4. //$test=mysubstr("cdb_account_log","_");
  5. //echo $test;
  6. ?>

PHP刪除資料庫表更改之處在:

1.開頭處

  1. function deldata($dbname,$tableflag){
  2. $db_host = 'localhost';
  3. $db_port = '3306';
  4. $db_user = 'user';
  5. $db_pass = 'password';

改為自己的資料庫地址,帳號和密碼即可

2.結尾處

  1. $dbname="shujukuming";//資料庫名
  2. $tableflag="xx";//要刪除的表的首碼,有這個首碼的表才刪除,標誌出是哪一個使用者
  3. deldata($dbname,$tableflag);
  4. //$test=mysubstr("cdb_account_log","_");
  5. //echo $test;
  6. ?>

改為自己的資料庫名和想刪掉的表首碼就可完成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刪除資料庫表具體實現代碼如下...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.