gpt4 book ai didi

php - 使用 mysqldump 创建备份

转载 作者:行者123 更新时间:2023-11-28 23:25:54 25 4
gpt4 key购买 nike

我想备份我的数据库,但它根本不起作用。

<?php
ob_start();

$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "dbmaurhotel";

$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table -host=$hostname
--user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
system($command);

$dump = ob_get_contents();
ob_end_clean();

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
date("Y-m-d_H-i-s").".sql"));
flush();
echo $dump;
exit();
?>

它下载了一个 sql 文件,但它不包含我的数据库中的任何内容。有人可以帮助我..谢谢

最佳答案

使用 shell_exec() 代替所有 ob_XXX 东西,它返回所有输出。

$username = "root"; 
$password = "";
$hostname = "localhost";
$dbname = "dbmaurhotel";

$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table -host=$hostname
--user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
$dump = shell_exec($command);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" . date("Y-m-d_H-i-s").".sql"));
echo $dump;
exit();

关于php - 使用 mysqldump 创建备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com