2015-08-14 34 views
0

从其他的具体情况不同,我发现这里的SO不能在PHP脚本运行mysql了shell_exec(.PHP)

我在主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

@BartFriederichs出于某种原因,我敢肯定,否则我不会问...这并不重要。关键是我问为什么这不会这样工作。我不想通过不同的代码结构来运行它。 – Fane

回答

2

documentation说:

通过shell执行命令,并返回完整输出作为一个字符串

它返回的输出。你从不使用输出,因此没有显示。你可以这样做:

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

不过,我劝你重新设计,以创建类或函数来分隔,而不是调用(相当昂贵)弹调用PHP代码的功能。

+0

我回显输出。没有显示。这在我的服务器启动后立即被调用。性能没问题。 – Fane

+0

@Fane你有什么错误吗? –

+0

无。当我改变路径并且得到“无法打开/找到文件xxx/another_script.php”时,我只得到一个错误。由于我没有得到这样的东西,我假设它是正确的...... – Fane