本文介绍了mysqli db 备份与 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当 mysql 工作时,我使用此代码来获取数据库的备份:
When mysql worked i used this code to get a backup of a database:
if (isset($_POST['getbackup'])) {
$db = "newsite";
$version = "2.3.1";
$date = date('Y-m-d H:i:s');
$final = "-- #".$version."#\n";
$final .= "-- database backup\n";
$final .= "--\n";
$final .= "-- PHP version: ".phpversion()."\n";
$final .= "-- MySQL version: ".mysqli_get_server_info($mysqli)."\n";
$final .= "-- Date: ".date("r")."\n";
$result = $mysqli->query("SHOW TABLE STATUS FROM ".$db);
while ($table = $result->fetch_array()) {
$i = 0;
$result2 = $mysqli->query("SHOW COLUMNS FROM $table[0]");
$z = $result2->num_rows;
$final .= "\n--\n-- DB Export - Table structure for table `".$table[0]."`\n--\n\nCREATE TABLE `".$table[0]."` (";
$prikey = false;
$insert_keys = null;
while ($row2 = $result2->fetch_array()) {
$i++;
$insert_keys .="`".$row2['Field']."`";
$final .= "`".$row2['Field']."` ".$row2['Type'];
if($row2['Null'] != "YES") { $final .= " NOT NULL"; }
if($row2['Default']) $final .= " DEFAULT '".$row2['Default']."'";
if($row2['Extra']) { $final .= " ".$row2['Extra']; }
if($row2['Key'] == "PRI") { $final .= ", PRIMARY KEY (`".$row2['Field']."`)"; $prikey = true; }
if($i < $z){
$final .= ", ";
$insert_keys .=", ";
}
else{
$final .= " ";
}
}
if($prikey) {
if($table[10]) $auto_inc = " AUTO_INCREMENT=".$table[10];
else $auto_inc = " AUTO_INCREMENT=1";
}
else $auto_inc = "";
$charset = explode("_", $table[14]);
$final .= ") ENGINE=".$table[1]." DEFAULT CHARSET=".$charset[0]." COLLATE=".$table[14].$auto_inc.";\n\n--\n-- DB Export - Dumping data for table `".$table[0]."`\n--\n";
$inhaltq = $mysqli->query("SELECT * FROM $table[0]");
while($inhalt = $inhaltq->fetch_array()) {
$final .= "\nINSERT INTO `$table[0]` (";
$final .= $insert_keys;
$final .= ") VALUES (";
for($i=0;$i<$z;$i++) {
$inhalt[$i] = str_replace("'","`", $inhalt[$i]);
$inhalt[$i] = str_replace("\\","\\\\", $inhalt[$i]);
$einschub = "'".$inhalt[$i]."'";
$final .= preg_replace('/\r\n|\r|\n/', '\r\n', $einschub);
if(($i+1)<$z) $final .= ", ";
}
$final .= ");";
}
$final .= "\n";
}
if($logged) {
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Description: File Transfer");
if(is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "msie")) AND is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "win" ))) header("Content-Disposition: filename=backup-".strtolower(date("D-d-M-Y")).".sql;");
else header("Content-Disposition: attachment; filename=backup-".strtolower(date("D-d-M-Y")).".sql;");
header("Content-Transfer-Encoding: binary");
}
}
它现在也可以工作了,当我单击按钮时,我得到了一个要下载的文件,但是该文件中没有 SQL 文件,而是页面的源代码在文件中..
It works now as well, i get a file to download when i click the button, BUT the file doesn't have SQL file in it, instead the source code of the page is in the file..
我做错了什么?
推荐答案
您没有输出 $final
数据.我正在使用此代码做完全相同的事情,试试吧...
You're not outputting your $final
data. I'm using this code to do exactly the same thing, try it...
header('Content-Description: File Transfer');
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $backup_filename . '.sql');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($final));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $final;
这篇关于mysqli db 备份与 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!