php 如何使用php脚本导出和导入mysql数据库及其数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10020022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export and import mysql database with its data using php script?
提问by Mohamed Hassan
I was asked to create an export and import mysql database with it's structure and data into a .sql file using php scripting and not phpmyadmin to make the user be able to backup his data?
我被要求使用 php 脚本而不是 phpmyadmin 创建一个导出并将其结构和数据导入到 .sql 文件中的 mysql 数据库,以使用户能够备份他的数据?
Does anybody have an idea how to do that??
有没有人知道如何做到这一点?
Thanks in advance
提前致谢
采纳答案by binarious
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
either
任何一个
$tableName = 'mypet';
$backupFile = 'backup/mypet.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
or
或者
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
回答by T.Todua
SAFE and WORKING SOLUTION:
安全和有效的解决方案:
EXPORT_TABLES("localhost","user","pass","db_name");
// has 5th and 6th optional parameters too.
Latest version is available at github: Export.php+ Import.php
最新版本可在 github 获得: Export.php+ Import.php
回答by Ayoub
Here is a nice class that worked just fine for me. if it says fopen access denied, try to change the folder permissions.
这是一个不错的课程,对我来说效果很好。如果它说 fopen 访问被拒绝,请尝试更改文件夹权限。

