MySQL Forums
Forum List  »  PHP

Using mysqldump via PHP system($command, $retval) does not work.
Posted by: rick pizzi
Date: May 15, 2016 12:51PM

Including code for two versions of mysqldump.

I run file1 as php -f file1.php and it works just fine.

I run file2 through an AJAX call to file2.php. I get a retval of 2 from the system() call.

I have set permissions on the directory and files to 777 so this shouldn't be a file permission problem.

// =============== file1 ====================


<?php

//Show errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');

$dbhost='localhost';
$dbuser='rick';
$dbpass='rick';
$dbname='pizzidb';

//$dbname=trim($_POST['db']); // call in progress through $.ajax
//$dir=trim($_POST['dir']);

$db='pizzidb';
$dir='/home/rick/DB-Web/pizzidb';

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$backupFile=$dir. "/".rtrim(strtoupper($db))."_DUMP_". date($format).".sql.gz";

$command = "/usr/bin/mysqldump --opt --add-drop-database --databases -h". $dbhost. " -u". $dbuser. " -p".$dbpass.
" ". $dbname. " | gzip > ". $backupFile;

$bak_result = system($command, $retval);

echo "retval = ".$retval."\n\n";

?>

// =========================== file2 =======================


<?php

//Show errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');

$dbhost='localhost';
$dbuser='rick';
$dbpass='rick';

$dbname=trim($_POST['db']); // call in progress through $.ajax
$dir=trim($_POST['dir']);

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$backupFile=$dir. "/".rtrim(strtoupper($dbname))."_DUMP_". date($format).".sql.gz";

$command = "/usr/bin/mysqldump --opt --add-drop-database --databases -h". $dbhost. " -u". $dbuser. " -p".$dbpass.
" ". $dbname. " | gzip > ". $backupFile;

$bak_result = system($command, $retval);

if ( $retval != 0) {

echo "bad";
}
else {

echo "File is stored in: ". $backupFile."\n";
}


?>

Options: ReplyQuote


Subject
Written By
Posted
Using mysqldump via PHP system($command, $retval) does not work.
May 15, 2016 12:51PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.