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