簡體   English   中英

使用PHP的Mysql Dump

[英]Mysql Dump using PHP

我正在嘗試編寫一個使用此代碼返回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,這意味着函數正確執行,否則我將獲得錯誤的返回值。

例如,我使用“系統”,因為它是我遇到的拳頭。 我如何獲得輸出信息嗎?

問候

由於可執行文件的路徑中有空格,因此需要將其用雙引號引起來。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM