PHP MySQL exec command not working - php

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.

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.

Related

Why is an integer argument passed to die() not printed?

Why do I get output of 60 from die() for this:
$min = $row['min'];
die($min);
But not for this?
$min = $row['min']*60;
die($min);
Solution
If you need the value even if it's a number, parse it to string with strval:
die(strval($min));
Documentation
According to die, it's an Equivalent to exit.
In Exit we can see that it has two signatures:
void exit ( int $status )
And
void exit ([ string $status ] )
Both are described below:
If status is an integer, that value will be used as the exit status
and not printed. Exit statuses should be in the range 0 to 254, the
exit status 255 is reserved by PHP and shall not be used. The status 0
is used to terminate the program successfully.
And
If status is a string, this function prints the status just before exiting.
(emphasis mine)
Elaborating
In the first code example from your question, $row['min'] is most likely just a string (eg string(2) "70"). When you multiply it by a number, PHP 'casts' the value to a number so it can multiply.
So, in the first example, it's calling the exit ([ string $status ] ) function, which prints the status just before exiting.
In the second example, it's calling the exit ( int $status ) function, which will be used as the exit status and not printed.
If you want to see it for yourself, use var_dump and check the variables types. Check an Example.
It is because you should pass a string: "Required. Specifies the message or status number to write before exiting the script. The status number will not be written to the output."
After multiplying the value is casted to int type and therefore die will not print it.
The die() function is an alias of exit().
PHP's documentation states that exit() is polymorphic. That is, it can accept both strings or integers as its only argument.
When you provide a string, it is printed before the script exits.
When you provide an integer (between 0-254), it is used as the script's exit status code and nothing is printed. A code of 0 denotes successful completion of the script, and anything else is an error. The code 255 is reserved by PHP.
You can use strval() or intval() to ensure you are passing the right data type for your needs.

PHP: why is shell_exec not working with unix time command?

I'm using shell_exec to execute a python file with a few variables and then print the real, user, and sys results to the console.
shell_exec("time /Users/$USER/anaconda/bin/python
/Applications/MAMP/cgi-bin/file.py
$var1 $var2 $var3", $result );
print_r($result);
Although this has worked for me before, it's not working now. The error I'm getting is PHP Warning: shell_exec() expects exactly 1 parameter, 2 given
It's the same response whether I just have time or /usr/bin/time.
What's going wrong here?
shell_exec() takes only a single parameter. What you were using before was likely exec().
What is in the command string that you're passing it (e.g. time or /usr/bin/time) is irrelevant to the warning you're getting.

Shell Script - PHP - display error

I'm using PHP function shell_exec()to execute Shell Script.
When I use this(correct) syntax, it displays output normally
echo shell_exec("ls -l");
But, for some reason, if user enters invalid command like this
echo shell_exec("lsl -l");
by default, it should give error as it gives on terminal. But, it doesn't display anything. is there any way to catch and display errors as well; via PHP?
That's because the output of the error goes to STDERR, while shell_exec only reads STDOUT. The simplest solution would probably be to pipe STDERR to STDOUT:
<?php
echo shell_exec('lsl -l 2>&1');
If you only want to know if the command was successful, I would use exec rather than shell_exec, as you can get the return value.
From the PHP docs:
This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.
If you choose to use exec() instead, you can supply a third param to the function which will return the output of the arguments:
exec ( string $command [, array &$output [, int &$return_var ]] )
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

What is the PHP exec() return value?

I am trying to use the PHP exec() function.
If the return_var argument is present along with the output argument,
then the return status of the executed command will be written to this
variable.
If the execution was successful, it's 0. However, if there is an error, it can be a multitude of other integers. I can't seem to find anywhere what those integers correspond to. How should I interpret the integer that I get?
Update:
I really should have specified this originally, but I am executing another PHP script. Unlike rsync, which has exit values on its man page, I can't find an equivalent for PHP.
So what I am doing is something like:
$rv = exec('php file.php', $out, $rv);
The return value is dependent on the process/program that you ran with exec. For instance, if you ran grep:
The exit status is 0 if selected lines are found, and 1 if not
found. If an error occurred the exit status is 2. (Note: POSIX
error handling code should check for '2' or greater.)
rsync has about 20 different error exit codes, all painstakingly explained in the man page:
http://linux.die.net/man/1/rsync
so yes, it's program-dependant :)
Even if you're running PHP script, the exit value depends on your program itself. By default php scripts will exit with 0. If you use the exit function you can return different exit codes:
http://php.net/manual/en/function.exit.php
If you want to experimentally determine what your php program exits, call it on the command line:
php file.php
then do
echo $?
this will show you the exit value of your php script.
IMHO, before use exec() function better set output and return_var parameters and read return code execution by return_var.
Don't rely on exec() return value.
Look up the manual page for the command that you are executing. This value has nothing to do with PHP but the actual command.

Calling perl from php?

I have a php script that handles a form input. For design reasons both a bit out of my control, and which I do not entirely wish to change, I have to call a perl script with the parameters specified in the html form.
I sanitized all inputs and then output them to a file called input, which is read by the perl script named, for sake of brevity in this question, script.pl. Script.pl should do some stuff and then write all outputs to a file named output.
I call the perl script from php like so:
system('perl script.pl 2>errors');
No good, nothing happens. output is not created, errors is not created, and the side effect does not occur.
My apache runs as www-data user and group id. My directory is set with 775 settings with ownership as me:www-data. (My user name is replaced with "me" for sakes for privacy).
My question is two fold:
1) Am I doing this wrong? If so how should I improve upon the code?
2) Is there a more sane way to catch errors in system execution?
After programming in perl for a while, php feels like a pain in the ass.
OS: Ubuntu server edition
popen can be used to get the shell's response. that is your best bet. Which can help you debug why system is angry. also, if your pl is saying "hello" and "bye", popen can even read that.
If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell
Ideally, I would have taken data from stdin and written to stdout. popen would allow neat access to both.
popen('pwd;perl script.pl 2>errors;echo done');
then you can see where were you (directory) when system got called and did it "done".
In the past I have used shell_exec() or backticks to accomplish this.
The documentation for shell_exec's return value indicates it is identical to the backtick operator:
Return Values
The output from the executed command.
Hope that helps.
system() only returns the status code.
$var = shell_exec ("ls");
print $var;
$var = `ls -l`;
print $var;
Is perl in the path? Maybe you need to specify it fully (e.g. /usr/bin/perl). Is system() returning false, indicating a failure? If you try something simpler, like system('/usr/bin/true', $retval), does $retval get set to 1?
Take a look at the PHP system() documentation. The following is the function prototype of system():
string system ( string $command [, int &$return_var ] )
Pass in a 2nd argument and then print out the return string as well as the second variable. See what the error says.

Categories