【问题标题】:Mysql Dump using PHP使用 PHP 的 Mysql 转储
【发布时间】:2015-05-12 08:08:14
【问题描述】:

我正在尝试使用以下代码编写一个返回整个 sql 转储的函数:

$command = "C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump --add-drop-table --host=$hostname
    --user=$username ";
if ($password) 
        $command.= "--password=". $password ." "; 
$command.= $dbname;
var_dump(terminal($command));

function terminal($cmd)
{
    if (function_exists('system'))
    {
        ob_start();
        system($cmd, $retVal);
        $output = ob_get_contents();
        ob_end_clean();
        $function_used = "system";
    }
    else if(function_exists('passthru'))
    {
        ob_start();
        passthru($cmd, $retVal);
        $output = ob_get_contents();
        ob_end_clean();
        $function_used = "passthru";
    }
    else if(function_exists('exec'))
    {
        exec($cmd, $output, $retVal);
        $output = implode("n", $output);
        $function_used = "exec";
    }
    else if(function_exists('shell_exec'))
    {
        $output = shell_exec($cmd);
        $function_used = "shell_exec";
    }
    else 
    {
        $output = "Cannot execute shell commands";
        $retVal = 1;
    }
     return array('output' => $output, 'returnValue' => $retVal, 'functionUsed' => $function_used);
}

当我在普通命令行中运行该命令时,数据库凭据是正确的,它会得到所需的结果。

问题是当我尝试使用这些命令时,无论我使用哪个函数,我都会得到一个空字符串作为输出。另一方面,返回值始终为 1,我认为结果为 true,这意味着该函数已正确执行,否则我将得到一个错误的返回值。

例如,我使用“系统”,因为它是我遇到的第一个。我得到输出的方式是否正确?

问候

【问题讨论】:

    标签: php mysql shell


    【解决方案1】:

    由于可执行文件的路径中有空格,因此需要用双引号将其括起来。

    $command = "\"C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump\" --add-drop-table --host=$hostname --user=$username ";
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2013-07-22
      • 1970-01-01
      • 2015-07-02
      相关资源
      最近更新 更多