【发布时间】: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,这意味着该函数已正确执行,否则我将得到一个错误的返回值。
例如,我使用“系统”,因为它是我遇到的第一个。我得到输出的方式是否正确?
问候
【问题讨论】: