Tag Archives: mysql

mysql 备份的PHP脚本

PHP代码
  1. <?php  
  2.     // 备份数据库  
  3.     function sqldumptable($table$fp=0) {  
  4.         $tabledump = "DROP TABLE IF EXISTS `$table`;\n";  
  5.         $tabledump .= "CREATE TABLE `$table` (\n";  
  6.   
  7.         $firstfield=1;  
  8.   
  9.         $fields = mysql_query("SHOW FIELDS FROM `$table`");  
  10.         while ($field = mysql_fetch_array($fields)) {  
  11.             if (!$firstfield) {  
  12.                 $tabledump .= ",\n";  
  13.             } else {  
  14.                 $firstfield=0;  
  15.             }  
  16.             $tabledump .= "   `$field[Field]` $field[Type]";  
  17.             if (!emptyempty($field["Default"])) {  
  18.                 if($field[‘Default’]!=‘CURRENT_TIMESTAMP’ ) $field[‘Default’] = "'{$field[‘Default’]}’";  
  19.                 $tabledump .= " DEFAULT $field[Default]";  
  20.             }  
  21.             if ($field[‘Null’] != "YES") {  
  22.                 $tabledump .= " NOT NULL";  
  23.             }  
  24.             if ($field[‘Extra’] != "") {  
  25.                 $tabledump .= " $field[Extra]";  
  26.             }  
  27.         }  
  28.         mysql_free_result($fields);  
  29.       
  30.         $keys = mysql_query("SHOW KEYS FROM `$table`");  
  31.         while ($key = mysql_fetch_array($keys)) {  
  32.             $kname=$key[‘Key_name’];  
  33.             if ($kname != "PRIMARY" and $key[‘Non_unique’] == 0) {  
  34.                 $kname="UNIQUE|$kname";  
  35.             }  
  36.             if(!is_array($index[$kname])) {  
  37.                 $index[$kname] = array();  
  38.             }  
  39.             $index[$kname][] = $key[‘Column_name’];  
  40.         }  
  41.         mysql_free_result($keys);  
  42.   
  43.         while(list($kname$columns) = @each($index)) {  
  44.             $tabledump .= ",\n";  
  45.             $colnames=implode($columns,",");  
  46.   
  47.             if ($kname == "PRIMARY") {  
  48.                 $tabledump .= "   PRIMARY KEY ($colnames)";  
  49.             } else {  
  50.                 if (substr($kname,0,6) == "UNIQUE") {  
  51.                     $kname=substr($kname,7);  
  52.                 }  
  53.                 $tabledump .= "   KEY $kname ($colnames)";  
  54.             }  
  55.         }  
  56.   
  57.         $tabledump .= "\n);\n\n";  
  58.         if ($fp) {  
  59.             fwrite($fp,$tabledump);  
  60.         } else {  
  61.             echo $tabledump;  
  62.         }  
  63.   
  64.         $rows = mysql_query("SELECT * FROM `$table`");  
  65.         $numfields = mysql_num_fields($rows);  
  66.         while ($row = mysql_fetch_array($rows)) {  
  67.             $tabledump = "INSERT INTO `$table` VALUES(";  
  68.   
  69.             $fieldcounter=-1;  
  70.             $firstfield=1;  
  71.             while (++$fieldcounter<$numfields) {  
  72.                 if (!$firstfield) {  
  73.                     $tabledump.=", ";  
  74.                 } else {  
  75.                     $firstfield=0;  
  76.                 }  
  77.   
  78.                 if (!isset($row[$fieldcounter])) {  
  79.                     $tabledump .= "NULL";  
  80.                 } else {  
  81.                     $tabledump .= "’".mysql_escape_string($row[$fieldcounter])."’";  
  82.                 }  
  83.             }  
  84.   
  85.             $tabledump .= ");\n";  
  86.   
  87.             if ($fp) {  
  88.                 fwrite($fp,$tabledump);  
  89.             } else {  
  90.                 echo $tabledump;  
  91.             }  
  92.         }  
  93.         mysql_free_result($rows);  
  94.         fwrite($fp,"\n\n\n");  
  95.     }  
  96.   
  97. ?>