简体   繁体   English

如何使用shell_exec函数在PHP中测试mysqldump是否成功

[英]How to test for success of mysqldump in PHP using shell_exec function

I am calling mysqldump via PHP to generate a backup of my database. 我正在通过PHP调用mysqldump来生成数据库的备份。

In my script I want to test whether or not the command completed successfully. 在我的脚本中,我想测试命令是否成功完成。

So far all of the solutions I have found use either 'exec' or 'system' to run mysqldump. 到目前为止,我发现的所有解决方案都使用“ exec”或“ system”运行mysqldump。

Those functions are disabled on my host, however 'shell_exec' is available. 这些功能在我的主机上被禁用,但是'shell_exec'可用。

Using shell_exec how can I check if the mysqldump command completed successfully, or if there was an error? 使用shell_exec如何检查mysqldump命令是否成功完成或是否有错误?

The value that shell_exec() returns is what the executed command outputs. shell_exec()返回的值是已执行命令的输出。

So, you should be getting all of the output from mysqldump inside of the return. 因此,您应该从返回的mysqldump获取所有输出。

Now, to check whether mysqldump failed or not, you will most likely want to do some search in the output string that correlates with the result. 现在,要检查mysqldump是否失败,您很可能希望在与结果相关的输出字符串中进行一些搜索。

Just a quick example. 只是一个简单的例子。 If you ran php -v in the console, you'd get something similar like this as a result: 如果您在控制台中运行了php -v ,结果将是类似的结果:

PHP 5.6.10 (cli) (built: Jun 10 2015 15:59:59)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

This is what you'd get as a result of shell_exec() : 这是您通过shell_exec()得到的结果:

string(148) "PHP 5.6.10 (cli) (built: Jun 10 2015 15:59:59) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies " 

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

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