无法在php脚本中使用mysql运行shell_exec(.php)

时间:2015-08-14 11:31:35

标签: php

与我在SO

上找到的其他情况不同

我在主php脚本中有以下代码

include("sql.php"); //holds all the data to mysql (and variable $db to connect)

$output = shell_exec('/usr/local/bin/php folder/another_script.php')

并在 another_script.php 我有这个:

include_once("../sql.php"); //notice difference to previous include in the other script

$query = "some query";
$db->query($query)
第一个脚本中的

$ output 为空。第二个脚本单独运行,而不是从另一个脚本调用。我错过了什么?

1 个答案:

答案 0 :(得分:2)

documentation说:

  

通过shell执行命令并将完整输出作为字符串

返回

返回输出。你永远不会使用输出,因此没有显示任何内容。你可以这样做:

$output = shell_exec('/usr/local/bin/php folder/another_script.php')
echo $output;

然而,我建议你重新考虑你的设计来创建类或函数来分离功能,而不是调用(非常昂贵的)shell来调用PHP代码。