我正在尝试使用以下代码备份数据库:
$conn = mysqli_connect("localhost", "root", "", "mjaudio");
define("BACKUP_PATH", "/mjaudio/uploads/");
$server_name = "localhost";
$username = "root";
$password = "";
$database_name = "mjaudio";
$date_string = date("Ymd");
$cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
exec($cmd);
if (mysqli_query($conn, $cmd)) {
echo "<div class='alert alert-success'> ";
echo "<strong>Backup Successfull</strong>";
echo "</div>";
} else {
echo "<div class='alert alert-danger'> ";
echo "<strong>Oops! Something went wrong!</strong>";
echo "Error: " .$cmd."<br>".mysqli_error($conn);
echo "</div>";
}
但是它给我一个错误:
Oops! Something went wrong!Error: mysqldump --routines -h localhost -u root -p mjaudio > /mjaudio/uploads/20180831_mjaudio.sql
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'mysqldump --routines -h localhost -u root -p mjaudio > /mjaudio/uploads/20180831' at line 1
我一直在尝试解决该查询,但似乎不起作用。我当前的Xampp控制面板是v3.2.2。版本有问题吗? 我该如何解决?
答案 0 :(得分:1)
问题是您尝试查询命令行。它不是SQL语句。其余代码看起来还可以
$conn = mysqli_connect("localhost", "root", "", "mjaudio");
define("BACKUP_PATH", "/mjaudio/uploads/");
$server_name = "localhost";
$username = "root";
$password = "";
$database_name = "mjaudio";
$date_string = date("Ymd");
$cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
if (exec($cmd)) {
echo "<div class='alert alert-success'> ";
echo "<strong>Backup Successfull</strong>";
echo "</div>";
} else {
echo "<div class='alert alert-danger'> ";
echo "<strong>Oops! Something went wrong!</strong>";
echo "Error with: " .$cmd;
echo "</div>";
}