码迷,mamicode.com
首页 > 数据库 > 详细

mysql 备份功能与恢复功能思路

时间:2015-05-21 12:26:28      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

备份

<?php

    $conn=mysql_connect("localhost","root","root") or die ("数据库连接失败");
    mysql_select_db("test",$conn) or die(mysql_error());
    mysql_query("set names utf8");
    $tabledump = "";
    $query = mysql_query("show tables",$conn);
    while($row = mysql_fetch_array($query)){
        $data[] = $row;
    }
    foreach ($data as $key => $value) {
        $tabledump .= "DROP TABLE IF EXISTS `$value[0]`;\n";
        $query1 = mysql_query("SHOW CREATE TABLE `$value[0]`",$conn);
        while($row1 = mysql_fetch_array($query1)){
            $data1[] = $row1;
        }
        foreach ($data1 as $key1 => $value1) {
            $tabledump .= $value1[1].";\n\n";
        }
    }
    file_put_contents("dump.sql", $tabledump);
?>

 恢复

<?php

    $conn=mysql_connect("localhost","root","root") or die ("数据库连接失败");
    mysql_select_db("test",$conn) or die(mysql_error());
    mysql_query("set names utf8");
    $sql = file_get_contents("dump.sql");
    mysql_query($sql,$conn);

?>

mysql 备份功能与恢复功能思路

标签:

原文地址:http://www.cnblogs.com/LoveJulin/p/4519048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!