LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

如何一键导出MySQL数据库

admin
2012年4月25日 9:25 本文热度 2805

有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?

以下是如何一键导出MySQL数据库的php代码。

新建一个名为backup.php的文件,复制粘贴以下代码,然后编辑数据库连接设置和mysqldump的路径。有必要的话,你还可以添加一个backup.php超链接到你的程序里:

1 <A href="backup.php">导出整个数据库</A>

请注意,第一个php代码执行的时候,会导出zip压缩后的sql文件,所以此代码所在文件夹需要可写的权限。
如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。

此代码需要可写权限:

01 <?php 
02     
03 $username = "root";  
04 $password = "";  
05 $hostname = "localhost";  
06 $dbname   = "cars"
07     
08 // if mysqldump is on the system path you do not need to specify the full path 
09 // simply use "mysqldump --add-drop-table ..." in this case 
10 $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql"
11 $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname 
12     --user=$username "; 
13 if ($password)  
14         $command.= "--password=". $password ." ";  
15 $command.= $dbname
16 $command.= " > " . $dumpfname
17 system($command); 
18     
19 // zip the dump file 
20 $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip"
21 $zip = new ZipArchive(); 
22 if($zip->open($zipfname,ZIPARCHIVE::CREATE))  
23
24    $zip->addFile($dumpfname,$dumpfname); 
25    $zip->close(); 
26
27     
28 // read zip file and send it to standard output 
29 if (file_exists($zipfname)) { 
30     header('Content-Description: File Transfer'); 
31     header('Content-Type: application/octet-stream'); 
32     header('Content-Disposition: attachment; filename='.basename($zipfname)); 
33     flush(); 
34     readfile($zipfname); 
35     exit
36
37 ?>

此代码不需要可写权限:

01 <?php 
02 ob_start(); 
03     
04 $username = "root";  
05 $password = "";  
06 $hostname = "localhost";  
07 $dbname   = "cars"
08     
09 // if mysqldump is on the system path you do not need to specify the full path 
10 // simply use "mysqldump --add-drop-table ..." in this case 
11 $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname 
12     --user=$username "; 
13 if ($password)  
14         $command.= "--password=". $password ." ";  
15 $command.= $dbname
16 system($command); 
17     
18 $dump = ob_get_contents();  
19 ob_end_clean(); 
20     
21 // send dump file to the output 
22 header('Content-Description: File Transfer'); 
23 header('Content-Type: application/octet-stream'); 
24 header('Content-Disposition: attachment; filename='.basename($dbname . "_" .  
25     date("Y-m-d_H-i-s").".sql")); 
26 flush(); 
27 echo $dump
28 exit();]]> 
29 ?>

该文章在 2012/4/25 9:25:48 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved