简体   繁体   English

PHP MySQL exec命令不起作用

[英]PHP MySQL exec command not working

I have written this code below which looks to see if a client is of a certain state and then exit if it is of a certain value. 我在下面编写了这段代码,该代码用于查看客户端是否处于特定状态,然后在客户端具有特定值时退出。 I have tested the query and syslog all variables being passed in to make sure they are not empty however, the return value of the exec command is 1. and $out is empty. 我已经测试了查询和syslog传入的所有变量,以确保它们不为空,但是exec命令的返回值为1,而$ out为空。

exec('/usr/bin/mysql --defaults-extra-file=database-user.cnf my_db -e "SELECT client_state FROM my_table WHERE transfer_id = '.$transfer_id.' AND client_id = '.$cid.'"', $out, $ret);

if($out == 15)
{
    syslog(LOG_INFO, "Transfer to client ".$client->getName()." has already been completed on this transfer")
    exit(EXIT_OK);
}

Beacause the output is empty it is skipping my if statement, any ideas why this command does not work and why the output is empty. 因为输出为空,所以它跳过了我的if语句,有关此命令为何不起作用以及输出为空的任何提示。

Look at the signature of the exec function: 查看exec函数的签名:

string exec ( string $command [, array &$output [, int &$return_var ]] )

The $out variable ( if your command is correct) will be an array, even if there's only 1 output line, it'll still be an array. $out变量( 如果您的命令正确)将是一个数组,即使只有1条输出行,它仍将是一个数组。
To know wether or not your command executed successfuly, you'll have to check your $ret variable's value. 要知道您的命令是否成功执行,您必须检查$ret变量的值。 If it's anything other than zero, whatever you executed didn't run smoothly. 如果它不是零,那么您执行的任何操作都不顺利。 A clean exit is marked by the program returning 0. 干净的退出由程序返回0标记。

I can't, though, for the life of me understand why you'd bother executing a query like this. 不过,我一辈子都无法理解为什么您要麻烦执行这样的查询。 If you have mysql running, and you have php installed, why not use mysqli_* or PDO ? 如果您正在运行mysql ,并且已安装php,为什么不使用mysqli_*PDO it'll save you a lot of hasstle, and it'll improve both performance and safety of your script. 它可以为您节省很多麻烦,并且可以提高脚本的性能和安全性。

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

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