Heim  >  Artikel  >  Backend-Entwicklung  >  php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)_PHP教程

php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)_PHP教程

WBOY
WBOYOriginal
2016-07-14 10:09:50858Durchsuche

[php] 
require_once('configuration.php'); 
$jconfig = new JConfig(); 
$connect = mysql_connect($jconfig->host ,$jconfig->user, $jconfig->password); 
$result = mysql_list_tables($jconfig->db);  
$tables = array(); 
while ($row = mysql_fetch_row($result)) { 
    $tables[] =  $row[0]; 

 
mysql_select_db($jconfig->db); 
$sql = ''; 
foreach($tables as $table){ 
    $sql .= backupTable($table); 

 
$r = file_put_contents('tmp/backup_'.date('Y-m-d-H-i-s').'.sql', $sql); 
 
if($r){ 
    die('success'); 
}else{ 
    die('lalala'); 

 
mysql_close($connect); 
 
function backupTable($table){ 
    $sqltxt = "DROP TABLE IF EXISTS $table;\n"; 
     
    $result = mysql_query("SHOW CREATE TABLE $table"); 
    $row = mysql_fetch_assoc($result); 
    $createsql = $row['Create Table']; 
    $sqltxt .= $createsql.";\n\n"; 
 
    $result = mysql_query("SELECT * FROM $table"); 
     
    $rows = array(); 
    while($row = mysql_fetch_assoc($result)){ 
        $fields = array(); 
        foreach($row as $field){ 
            $fields[] = '\''.mysql_escape_string($field).'\''; 
        } 
         
        $rows[] = '('.implode(',', $fields).')'; 
    } 
    if(!emptyempty($rows)){ 
        $sqltxt .= "INSERT INTO `$table` VALUES".implode(",\n", $rows).";\n"; 
    } 
     
     
    $sqltxt .= "\n"; 
 
    return $sqltxt; 

require_once('configuration.php');
$jconfig = new JConfig();
$connect = mysql_connect($jconfig->host ,$jconfig->user, $jconfig->password);
$result = mysql_list_tables($jconfig->db);
$tables = array();
while ($row = mysql_fetch_row($result)) {
 $tables[] =  $row[0];
}

mysql_select_db($jconfig->db);
$sql = '';
foreach($tables as $table){
 $sql .= backupTable($table);
}

$r = file_put_contents('tmp/backup_'.date('Y-m-d-H-i-s').'.sql', $sql);

if($r){
 die('success');
}else{
 die('lalala');
}

mysql_close($connect);

function backupTable($table){
 $sqltxt = "DROP TABLE IF EXISTS $table;\n";
 
 $result = mysql_query("SHOW CREATE TABLE $table");
 $row = mysql_fetch_assoc($result);
 $createsql = $row['Create Table'];
 $sqltxt .= $createsql.";\n\n";

 $result = mysql_query("SELECT * FROM $table");
 
 $rows = array();
 while($row = mysql_fetch_assoc($result)){
  $fields = array();
  foreach($row as $field){
   $fields[] = '\''.mysql_escape_string($field).'\'';
  }
  
  $rows[] = '('.implode(',', $fields).')';
 }
 if(!empty($rows)){
  $sqltxt .= "INSERT INTO `$table` VALUES".implode(",\n", $rows).";\n";
 }
 
 
 $sqltxt .= "\n";

 return $sqltxt;
}

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477589.htmlTechArticle[php] ?php require_once(configuration.php); $jconfig = new JConfig(); $connect = mysql_connect($jconfig-host ,$jconfig-user, $jconfig-password); $result = mysql_list_tables($jconfi...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn