Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > Advice with some code
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 1st November 06:04
sf
External User
 
Posts: 1
Default Advice with some code



Hi all,


I have a block of code that looks like this:

public function backup() {
$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);
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:
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);
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? :-)

Thanks for your advice.
  Reply With Quote


 


2 1st November 06:05
robert
External User
 
Posts: 1
Default Advice with some code



You can use try/catch, or since your current semantics are to return
boolean only, you could check do the following:

<?php

if( ($status = backup()) !== true )
{
if( is_string( $status ) )
{
echo 'Failure: '.$status."\n"; } }
?>

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Reply With Quote
3 1st November 06:05
External User
 
Posts: 1
Default Advice with some code


Return an array,

one element is the result
the other is an error message (if any).

if the ['err'] element is not set then the data is good... :-)
  Reply With Quote
4 1st November 06:05
jochem
External User
 
Posts: 1
Default Advice with some code


public function backup(&$errors)

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


if($mysqlDump->backup($errMsgs)) {

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


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.
  Reply With Quote
Reply


Thread Tools
Display Modes




666