PHP備份資料庫為SQL檔案 並可下載源碼

來源:互聯網
上載者:User

  這是一個將指定資料庫裡的所有表備份為一個SQL檔案,可下載。這個源碼來自dedecms程式,功能挺多,也很實用,但是代碼的品質還有待提高

  1. <!?php    
  2.     
  3. /******   備份資料庫結構 ******/    
  4.       
  5.       /*   
  6.       函數名稱:table2sql()   
  7.       函數功能:把表的結構轉換成為SQL   
  8.       函數參數:$table: 要進行提取的表名   
  9.       返 回 值:返回提取後的結果,SQL集合   
  10.       函數作者:heiyeluren   
  11.       */    
  12.       
  13.      function table2sql($table)    
  14.       {    
  15.           global $db;    
  16.          $tabledump = "DROP TABLE IF EXISTS $table;\n";    
  17.          $createtable = $db--->query("SHOW CREATE TABLE $table");    
  18.          $create = $db->fetch_row($createtable);    
  19.          $tabledump .= $create[1].";\n\n";    
  20.           return $tabledump;    
  21.       }    
  22.       
  23.       
  24.      /****** 備份資料庫結構和所有資料 ******/    
  25.       /*   
  26.       函數名稱:data2sql()   
  27.       函數功能:把表的結構和資料轉換成為SQL   
  28.       函數參數:$table: 要進行提取的表名   
  29.       返 回 值:返回提取後的結果,SQL集合   
  30.       函數作者:heiyeluren   
  31.       */    
  32.      function data2sql($table)    
  33.       {    
  34.           global $db;    
  35.          $tabledump = "DROP TABLE IF EXISTS $table;\n";    
  36.          $createtable = $db->query("SHOW CREATE TABLE $table");    
  37.          $create = $db->fetch_row($createtable);    
  38.          $tabledump .= $create[1].";\n\n";    
  39.       
  40.          $rows = $db->query("SELECT * FROM $table");    
  41.          $numfields = $db->num_fields($rows);    
  42.          $numrows = $db->num_rows($rows);    
  43.           while ($row = $db->fetch_row($rows))    
  44.           {    
  45.              $comma = "";    
  46.              $tabledump .= "INSERT INTO $table VALUES(";    
  47.               for($i = 0; $i < $numfields; $i++)    
  48.               {    
  49.                  $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";    
  50.                  $comma = ",";    
  51.               }    
  52.              $tabledump .= ");\n";    
  53.           }    
  54.          $tabledump .= "\n";    
  55.       
  56.           return $tabledump;    
  57.       }    
  58. ?>    
  59.     
  60. <!--?php         
  61. $host="localhost"; //主機名稱         
  62. $user="root"; //MYSQL使用者名稱         
  63. $password="root"; //密碼         
  64. $dbname="dedecmsv4"; //備份的資料庫         
  65.        
  66. mysql_connect($host,$user,$password);         
  67. mysql_select_db($dbname);         
  68.        
  69. $q1=mysql_query("show tables");         
  70. while($t=mysql_fetch_array($q1)){         
  71. $table=$t[0];         
  72. $q2=mysql_query("show create table `$table`");         
  73. $sql=mysql_fetch_array($q2);         
  74. $mysql.=$sql['Create Table'].";\r\n\r\n";#DDL         
  75.        
  76. $q3=mysql_query("select * from `$table`");         
  77. while($data=mysql_fetch_assoc($q3))         
  78. {         
  79. $keys=array_keys($data);         
  80. $keys=array_map('addslashes',$keys);         
  81. $keys=join('`,`',$keys);         
  82. $keys="`".$keys."`";         
  83. $vals=array_values($data);         
  84. $vals=array_map('addslashes',$vals);         
  85. $vals=join("','",$vals);         
  86. $vals="'".$vals."'";         
  87.        
  88. $mysql.="insert into `$table`($keys) values($vals);\r\n";         
  89. }         
  90. $mysql.="\r\n";         
  91.        
  92. }         
  93. $filename=date('Ymd')."_".$dbname.".sql"; //檔案名稱為當天的日期         
  94. $fp = fopen($filename,'w');         
  95. fputs($fp,$mysql);         
  96. fclose($fp);         
  97. echo "資料備份成功,產生備份檔案".$filename;         
  98. ?>   




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.