Steve Finkelstein wrote:
> Hi all,
> 
> I'm having a brain freeze with some simple code that I wrote and now trying
> to refactor.
> 
> I have a block of code that looks like this:
> 
>     public function backup()

public function backup(&$errors)

>       {
>           $fname = "$this->dbName.sql.$this->zip";
> 
>           $this->cmd = "mysqldump -Q -u $this->dbUser -p$this->dbPass
> $this->dbName 2>&1 > {$this->dest}/{$this->dbName}.{$this->lastId}.sql";
> 
> 
> 
>         $res = shell_exec("$this->cmd");
>           error_log("First res: ".$res);
> 
>             if(!$res) {
>                   $this->cmd = ($this->zip==="bz2") ?
>                         "bzip2
> {$this->dest}/{$this->dbName}.{$this->lastId}.sql 2>&1"  :
>                         "gzip
> {$this->dest}/{$this->dbName}.{$this->lastId}.sql 2>&1";
> 
>                     $res = shell_exec("$this->cmd");
>                   error_log("second error: ".$res);

                        $errors[] = "second error: ".$res;

>                     return !$res;
>             }
> 
>             return FALSE;
>     }
> 
> Now instead of that FALSE, is there a way I can pass FALSE with a particular
> error message?  This is because on the other end I have code that looks like
> this:
> 

$errMsgs = array();
if($mysqlDump->backup($errMsgs)) {

>       if($mysqlDump->backup()) {
>             $success = array('success' => '1');
>             $sqlres = mysql_query($sql, $link) or
> die(json_message('error',mysql_error()));
>             shell_exec('/usr/bin/touch /tmp/build_transfer');
>             mysql_close($link);
>             return  '(' . json_encode($success) . ')';
>       } else {
>             $fail = array('fail' => $res);

$fail = array('fail' => $res, 'errors' = $errMsgs);

>             return '(' . json_encode($fail) . ')';
>       }
> 
> I'd ultimately like to be able to deliver a failure message from the return
> value...and properly catch that message so I can send it back in JSON format
> to the client browser to report what the error is.
> 
> Think I should approach this with some try{..} catch code? Am I overlooking
> something really simple? :-)

simple enough? the basic idea is the same as passing a variable by reference to
exec() as the second argument in order to capture output. so your func has
a return value to determine status and you can pass in an array to capture 
detailed
process related messages.

> 
> Thanks for your advice.
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to