search
Homephp教程PHP源码php 数据库在线备份代码

<script>ec(2);</script>

php 数据库在线备份代码

function sqldumptable($table, $fp=0) {
  $tabledump = "DROP TABLE IF EXISTS $table;n";
  $tabledump .= "CREATE TABLE $table (n";

  $firstfield=1;

  $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"])) {
    $tabledump .= " DEFAULT '$field[Default]'";
   }
   if ($field['Null'] != "YES") {
    $tabledump .= " NOT NULL";
   }
   if ($field['Extra'] != "") {
    $tabledump .= " $field[Extra]";
   }
  }
  mysql_free_result($fields);
 
  $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);

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

   if ($kname == "PRIMARY") {
    $tabledump .= "   PRIMARY KEY ($colnames)";
   } else {
    if (substr($kname,0,6) == "UNIQUE") {
     $kname=substr($kname,7);
    }
    $tabledump .= "   KEY $kname ($colnames)";
   }
  }

  $tabledump .= "n);nn";
  if ($fp) {
   fwrite($fp,$tabledump);
  } else {
   echo $tabledump;
  }

  $rows = mysql_query("SELECT * FROM $table");
  $numfields = mysql_num_fields($rows);
  while ($row = mysql_fetch_array($rows)) {
   $tabledump = "INSERT INTO $table VALUES(";

   $fieldcounter=-1;
   $firstfield=1;
   while (++$fieldcounter     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);
 }


@mysql_connect($servername,$dbusername,$dbpassword) or die("数据库连接失败");
 @mysql_select_db($dbname) or die("选择数据库失败"); 
 $table = array_flip($_POST['table']);
 $result = mysql_query("SHOW tables");
 echo ($result) ? NULL : "出错: ".mysql_error();

 $filename = basename($_SERVER['HTTP_HOST']."_MySQL.sql");
 header('Content-type: application/unknown');
 header('Content-Disposition: attachment; filename='.$filename);
 $mysqldata = '';
 while ($currow = mysql_fetch_array($result)) {
  if (isset($table[$currow[0]])) {
   $mysqldata.= sqldumptable($currow[0]);
   $mysqldata.= $mysqldata."rn";
  }
 }
 mysql_close();
 exit;

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools