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

php中mysql数据库备份

时间:2015-11-05 10:31:25      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

<?php
// 备份数据库
$host = "localhost";
$user = "root"; //数据库账号
$password = ""; //数据库密码
$dbname = "test"; //数据库名称
// 这里的账号、密码、名称都是从页面传过来的
if (!mysql_connect($host, $user, $password)) // 连接mysql数据库
    {
        echo ‘数据库连接失败,请核对后再试‘;
    exit;
}
if (!mysql_select_db($dbname)) // 是否存在该数据库
    {
        echo ‘不存在数据库:‘ . $dbname . ‘,请核对后再试‘;
    exit;
}
mysql_query("set names ‘utf8‘");
$mysql = "set charset utf8;\r\n";
$q1 = mysql_query("show tables");
while ($t = mysql_fetch_array($q1))
{
    $table = $t[0];
    $q2 = mysql_query("show create table `$table`");
    $sql = mysql_fetch_array($q2);
    $mysql .= $sql[‘Create Table‘] . ";\r\n";
    $q3 = mysql_query("select * from `$table`");
    while ($data = mysql_fetch_assoc($q3))
    {
        $keys = array_keys($data);
        $keys = array_map(‘addslashes‘, $keys);
        $keys = implode(‘`,`‘, $keys);
        $keys = "`" . $keys . "`";
        $vals = array_values($data);
        $vals = array_map(‘addslashes‘, $vals);
        $vals = join("‘,‘", $vals);
        $vals = "‘" . $vals . "‘";
        $mysql .= "insert into `$table`($keys) values($vals);\r\n";
    }
}
 
$filename = $dbname . date(‘Ymjgi‘) . ".sql"; //存放路径,默认存放到项目最外层
$fp = fopen($filename, ‘w‘);
fwrite($fp, $mysql);
fclose($fp);
echo "数据备份成功";
 
?>

  

php中mysql数据库备份

标签:

原文地址:http://www.cnblogs.com/relix/p/4938338.html

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