2012-04-04 93 views
14

我被要求创建一个导出和导入mysql数据库的结构和数据到一个.sql文件使用PHP脚本而不是phpmyadmin,使用户能够备份他的数据?如何使用php脚本导出和导入mysql数据库的数据?

有没有人有一个想法该怎么做?

在此先感谢

+0

是否必须是公正PHP?总是可以使用mysqldump,如果需要成为PHP脚本,可以通过exec使用mysqldump。 – Corbin 2012-04-04 22:17:14

+1

[无法访问服务器或phpMyADMIN的简单方法导出SQL表](http://stackoverflow.com/q/81934/),[php代码导出到mysql数据库](http://stackoverflow.com/q/2491728/90527),[最佳实践:使用PHP导入mySQL文件;拆分查询](http://stackoverflow.com/q/1883079/90527),并可能[许多其他](http://stackoverflow.com/search?q=PHP+MySQL+%2Bexport+%2Bimport)。 – outis 2012-04-04 22:22:01

回答

10

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx

要么

$tableName = 'mypet'; 
$backupFile = 'backup/mypet.sql'; 
$query  = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName"; 
$result = mysql_query($query); 

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz'; 
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip >  $backupFile"; 
system($command); 
+0

任何人都知道如何通过php脚本将其导入到phpmyadmin – 2012-04-05 00:31:17

+0

看看教程。有一个''LOAD DATA INFILE'backupFile'INTO TABLE $ tableName“;'部分。 – binarious 2012-04-05 00:32:54

14

安全和有效的解决方案(最新版本: Export.php + Import.php

EXPORT_TABLES("localhost","user","pass","db_name"); 
                 //or add 5th parameter(array) of specific tables: array("mytable1","mytable2","mytable3") 

CODE:

<?php 
//https://github.com/tazotodua/useful-php-scripts 
function EXPORT_TABLES($host,$user,$pass,$name, $tables=false, $backup_name=false){ 
    $mysqli = new mysqli($host,$user,$pass,$name); $mysqli->select_db($name); $mysqli->query("SET NAMES 'utf8'"); 
    $queryTables = $mysqli->query('SHOW TABLES'); while($row = $queryTables->fetch_row()) { $target_tables[] = $row[0]; } if($tables !== false) { $target_tables = array_intersect($target_tables, $tables); } 
    foreach($target_tables as $table){ 
     $result = $mysqli->query('SELECT * FROM '.$table); $fields_amount=$result->field_count; $rows_num=$mysqli->affected_rows;  $res = $mysqli->query('SHOW CREATE TABLE '.$table); $TableMLine=$res->fetch_row(); 
     $content = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n"; 
     for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) { 
      while($row = $result->fetch_row()) { //when started (and every after 100 command cycle): 
       if ($st_counter%100 == 0 || $st_counter == 0) {$content .= "\nINSERT INTO ".$table." VALUES";} 
        $content .= "\n("; 
        for($j=0; $j<$fields_amount; $j++) { $row[$j] = str_replace("\n","\\n", addslashes($row[$j])); if (isset($row[$j])){$content .= '"'.$row[$j].'"' ; }else {$content .= '""';}  if ($j<($fields_amount-1)){$content.= ',';}  } 
        $content .=")"; 
       //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler 
       if ((($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) {$content .= ";";} else {$content .= ",";} $st_counter=$st_counter+1; 
      } 
     } $content .="\n\n\n"; 
    } 
    $backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql"; 
    header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$backup_name."\""); echo $content; exit; 
} 
?> 
+1

谢谢!这是一个最好的代码。因为其他代码与“exec”或“系统”功能一起工作。这些功能在Linux主机上被禁用。但是这个脚本在Linux主机上工作。 – mghhgm 2014-04-19 22:56:37

+0

exellent代码为我工作 – Nothing 2014-05-22 07:35:43

+0

真棒...感谢您分享此代码Mr.tazo ...它的工作完美的我...谢谢。 – 2016-10-02 12:08:21