TCM
TCM

Reputation: 16900

Filesize 0 mysqldump

I am using Windows 7 and using php i am backing up my database with the help of mysqldump. The file gets successfully written but the size remains always 0. Any idea why is this happening?

Note :- If i type the same command in command line, it works. This is my function :-

   public static function BackupDatabase($backupPath){
        $fileName = uniqid() .'.sql';
        $backupCommand = 'mysqldump -u ' . DBUsername .' -p' . DBPassword .' abc >' . $backupPath . $fileName  ;

        $retVal = '';
        $feedback = system($backupCommand, $retVal);
        if($feedback == NULL || $feedback == '')
            return 'Database backed up successfully by name ' . $fileName;
        else
            return $feedback;
    }

EDIT :-

public static function BackupDatabase($backupPath){
        $fileName = uniqid() .'.sql';
        $backupCommand = 'mysqldump -u ' . DBUsername . ' abc > ' . $backupPath . $fileName .' 2>&1'  ;
        echo $backupCommand;
        $retVal = '';
        $feedback = system($backupCommand, $retVal);
        echo $retVal;
        if($feedback == NULL || $feedback == '')
            return 'Database backed up successfully by name ' . $fileName;
        else
            return $feedback;
    }

Thanks in advance :)

Upvotes: 0

Views: 817

Answers (3)

user261875
user261875

Reputation: 31

After much headache, this syntax worked for me:

cmd /c " C:\mysqldump.exe -h $mysql_server -u $mysql_user --password=$mysql_password $dbname > C:\Dump.sql "

Upvotes: 0

ajreal
ajreal

Reputation: 47321

I believe is caused by the environment setting.
You can execute mysqldump successfully in the command line as you probably has mysqldump register in your environment.

While running in a PHP, the path to mysqldump might not recognized by user that running the web server.

And

$feedback = system('unknown program > file', $retval);

Will always set $feeback = ''; even PHP cannot find where is your mysqldump program, it still pipe a single character to backup file.

You can put in absolute path to mysqldump to test again.

Upvotes: 1

Flinsch
Flinsch

Reputation: 4341

Check your $backupCommand string. It seems to me that there are some spaces missing (e.g. in the vicinity of '-p').

Upvotes: 0

Related Questions