Charabon
Charabon

Reputation: 787

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.

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.

Upvotes: 0

Views: 795

Answers (1)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76415

Look at the signature of the exec function:

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.
To know wether or not your command executed successfuly, you'll have to check your $ret variable's value. If it's anything other than zero, whatever you executed didn't run smoothly. A clean exit is marked by the program returning 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? it'll save you a lot of hasstle, and it'll improve both performance and safety of your script.

Upvotes: 1

Related Questions