程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 備份mysql數據庫(joomla數據庫可直接使用,其他數據庫稍作修改即可)

php 備份mysql數據庫(joomla數據庫可直接使用,其他數據庫稍作修改即可)

編輯:關於PHP編程

[php] 
<?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; 

<?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(!empty($rows)){
  $sqltxt .= "INSERT INTO `$table` VALUES".implode(",\n", $rows).";\n";
 }
 
 
 $sqltxt .= "\n";

 return $sqltxt;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved