一段备份数据库的代码 - 我爱贝贝 - 金东浩官方网站 - welcome to old.bbmy.net

一段备份数据库的代码 [金东浩官方网站版权所有]

2006年12月31日 | Category: » | by: 贝爸 | 本文包含金东浩一段备份数据库的代码照片


function sqldumptable($table, $fp=0) {

$tabledump = "DROP TABLE IF EXISTS $table;\\n";
$tabledump .= "CREATE TABLE $table (\\n";

$firstfield=1;

// get columns and spec
$fields = mysql_query("SHOW FIELDS FROM $table"

while ($field = mysql_fetch_array($fields)) {
 if (!$firstfield) {
  $tabledump .= ",\\n";
 } else {
  $firstfield=0;
 }
 $tabledump .= "   $field[Field] $field[Type]";
 if (!empty($field["Default"])) {
  // get default value
  $tabledump .= " DEFAULT ''$field[Default]''";
 }
 if ($field[''Null''] != "YES") {
  // can field be null
  $tabledump .= " NOT NULL";
 }
 if ($field[''Extra''] != "") {
  // any extra info?
  $tabledump .= " $field[Extra]";
 }
}
mysql_free_result($fields


// get keys list
$keys = mysql_query("SHOW KEYS FROM $table"

while ($key = mysql_fetch_array($keys)) {
 $kname=$key[''Key_name''];
 if ($kname != "PRIMARY" and $key[''Non_unique''] == 0) {
 $kname="UNIQUE|$kname";
 }
 if(!is_array($index[$kname])) {
 $index[$kname] = array(

 }
 $index[$kname][] = $key[''Column_name''];
}
mysql_free_result($keys


// get each key info
while(list($kname, $columns) = @each($index)){
      $tabledump .= ",\\n";
      $colnames=implode($columns,","


      if ($kname == "PRIMARY") {
 // do primary key
 $tabledump .= "   PRIMARY KEY ($colnames)";
      } else {
 // do standard key
 if (substr($kname,0,6) == "UNIQUE") {
    // key is unique
    $kname=substr($kname,7

 }
 $tabledump .= "   KEY $kname ($colnames)";
      }
}

$tabledump .= "\\n
\\n\\n";
if ($fp) {
    fwrite($fp,$tabledump

} else {
    echo $tabledump;
}

// get data
$rows = mysql_query("SELECT * FROM $table"

// $numfields=$DB->num_fields($rows

$numfields = mysql_num_fields($rows

while ($row = mysql_fetch_array($rows)) {
 $tabledump = "INSERT INTO $table VALUES(";

 $fieldcounter=-1;
 $firstfield=1;
 // get each field''s data
 while (++$fieldcounter<$numfields) {
     if (!$firstfield) {
  $tabledump.=", ";
     } else {
  $firstfield=0;
     }

     if (!isset($row[$fieldcounter])) {
  $tabledump .= "NULL";
     } else {
  $tabledump .= "''".mysql_escape_string($row[$fieldcounter])."''";
     }
 }

 $tabledump .= "
\\n";

 if ($fp) {
  fwrite($fp,$tabledump

 } else {
  echo $tabledump;
 }
}
mysql_free_result($rows

}

//backup

//这是一个备份数据库中所以表的循环

/*if ($_POST[action]=="dobackup") {

   $table = array_flip($_POST[table]


   $filehandle = fopen($path,"w"

   $result = $DB->query("SHOW tables"

   while ($currow = $DB->fetch_array($result)) {
          if (isset($table[$currow[0]])) {
              sqldumptable($currow[0], $filehandle

              fwrite($filehandle,"\\n\\n\\n"

          }
   }
   fclose($filehandle

   pa_exit("数据库已备份"

}
*/

//这是备份一个表"a"

$filehandle = fopen($path,"w")

sqldumptable("a", $filehandle)

fclose($filehandle)

本文来自 我爱贝贝[www.bbmy.net]-金东浩官方网站,如若转载注明出处。


金东浩博客随机日志
金东浩博客之跟奶奶睡
跟奶奶睡
金东浩博客之杏花放风筝
杏花放风筝
金东浩博客之胜利广场的脚印
胜利广场的脚印
金东浩博客之流星枪
流星枪
金东浩博客之还是不快乐
还是不快乐
金东浩博客之逍遥津游玩
逍遥津游玩
金东浩博客之和美女拍照
和美女拍照
金东浩博客之吹泡泡
吹泡泡
金东浩博客之豹子装
豹子装
金东浩博客之外婆家回来
外婆家回来
金东浩博客之六一表演礼服
六一表演礼服
金东浩博客之补:和两位美女过家家
补:和两位美女过
金东浩博客之又生病了
又生病了
金东浩博客之巧遇小胖墩
巧遇小胖墩
金东浩博客之吃西瓜
吃西瓜
金东浩博客之端午节挂彩
端午节挂彩
金东浩博客之跷跷板
跷跷板
金东浩博客之脆弱的身体
脆弱的身体

金东浩日记Tags: , ,
金东浩日记评论: 0 | 金东浩日记引用: 0 | 金东浩日记阅读: 3121
发表评论
昵称 [注册]        网址
密码 游客无需密码     电邮
打开HTML 打开UBB 打开表情 隐藏 记住我