简体   繁体   English

PHP中exec()的输出为空数组

[英]Output of exec() in php is empty array

I have the following code to execute mysqldump using exec() . 我有以下代码使用exec()执行mysqldump It was not working and I wanted figure out what output the command line throws. 它没有用,我想弄清楚命令行抛出什么输出。 So I printed $output and $worked . 所以我打印了$output$worked But all I get is 1 and an empty array . 但是我得到的只是1和一个空数组

Accordong to manual: If the output argument is present, then the specified array will be filled with every line of output from the command. 按照手册进行操作: If the output argument is present, then the specified array will be filled with every line of output from the command. But why is this throwing an empty array? 但是为什么这会抛出一个空数组? I am including 2>&1 at the end of command as some post suggested, but still not working. 正如某些帖子所建议的那样,我在命令末尾包含2>&1 ,但仍然无法正常工作。 I want to know what the terminal is outputting when this command is being executed. 我想知道执行此命令时终端输出的内容。

And what does $worked outputting 1 mean? $worked输出1是什么意思?

Any help will be greatly appreciated! 任何帮助将不胜感激!

Script: 脚本:

$command='mysqldump --opt -u ' .$db['username'] .' -p' .$db['password'] .' --databases ' .$db['database'] .' > '.$mysqlExportPath .' 2>&1';
$output=array();
exec($command,$output,$worked);

print $worked.'<br>';
print_r($output);

Outputs: 输出:

1
Array ( ) 

I want to know what the terminal is outputting when this command is being executed. 我想知道执行此命令时终端输出的内容。

Nothing ! 没事 (And had you simply tried it yourself you'd have seen that immediately). (如果您只是自己尝试过,您会立即看到的)。 Your command directs the result of mysqldump to the file at the path denoted by $mysqlExportPath . 您的命令将mysqldump的结果定向到$mysqlExportPath表示的路径中的文件。 I don't know why you expect it to go to stdout instead. 我不知道您为什么期望它转为标准输出

Perhaps some line breaks would help to make your code a little (no, substantially !) more readable: 也许一些换行符可以使您的代码更具可读性(不, 实质上是 !):

$command
  = 'mysqldump --opt'
  . ' -u ' . $db['username']
  . ' -p' . $db['password']
  . ' --databases ' . $db['database']
  . ' > ' . $mysqlExportPath   // <---- Here you go!
  . ' 2>&1';

Remove the redirection, or find the data inside the file $mysqlExportPath ; 删除重定向,或在文件$mysqlExportPath找到数据; your call! 您的来电!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM