备份
<?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); ?>
时间: 2024-10-28 20:44:18