使用php错误备份mysql数据库

pzfprimi  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(467)

我正在尝试使用以下代码备份我的数据库:

$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。版本有问题吗?我怎样才能解决这个问题?

twh00eeo

twh00eeo1#

问题是您试图查询您的命令行。它不是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>";
}

相关问题