You mentioned that you had other methods preceding this that also make use of exec
... and thus your logic is flawed. Every time you execute a command using exec
, your working in a fresh blank environment, and the previous session's data is no longer available; and as such $?
will not have the value you're looking for. You need to stop trying to write bash scripts inside of PHP.
For example, in one of your comments you mentioned the following:
i did this : $Backup = "mysqldump .........."; exec($Backup); $testvar
='.............'; exec($testvar); $createzip ='..........'; exec($createzip);
When instead of trying to execute bash commands as a variable... you should be executing only the necessary commands outside of PHP.
instead of something like
$testvar = '
/usr/bin/mysqldump -u DBuser -pDBpassword database >database.sql 2>database.err
if [ "$?" -eq 0 ]
then
echo "Success"
else
echo "Mysqldump encountered a problem look in database.err for information"
fi
';
$MysqlDumpMsg=exec($testvar);
echo $MysqlDumpMsg;
You should be doing something more like this:
exec("mysqldump .......... 2>database.err",$MysqlDumpMsg,$MysqlDumpCode);
if ($MysqlDumpCode = 0) {
echo "Success";
} else {
echo "Mysqldump encountered a problem look in database.err for information";
}
or whatever else you need. If you have some zip'ing command in there someplace... then do it as several steps...
exec("mysqldump .......... >database.sql 2>database.err",$MysqlDumpMsg,$MysqlDumpCode);
if ($MysqlDumpCode = 0) {
echo "Successfully dumped database.";
exec("gzip database.sql", $ZipMsg, $ZipReturnCode)
if ($ZipReturnCode = 0) {
echo "Successfully zip'd database!";
} else {
echo "Failed to compress the database."
}
} else {
echo "Mysqldump encountered a problem look in database.err for information";
}