views:

106

answers:

2

I write some code with PHP to backup database.

Here my code:

exec("mysqldump --opt -h localhost -u root test > mydb.sql");

But I get 0-byte in my file (mydb.sql). I also run with passthru(), system(), but it still gets 0-byte.

I try to use command. It works.

I use the lastest XAMPP for my localhost.

So, how can I do to make it works correctly?

+3  A: 

It's likely a permissions issue, or the fact you're not passing a password. To see errors, route STDERR to STDOUT using 2>&1

exec("mysqldump --opt -h localhost -u root test > mydb.sql 2>&1", $output);
print_r($output);

This will show you the errors you'd normally see on the command line.

Andy
A: 

Most likely mysqldump is not in PHP/Apache's path. That will cause the shell to spit out a "command not found" error on STDERR, and no output on STDOUT (which you redirect to a file and ends up with 0 length).

Try adding the full path to mysqldump (c:\mysql\bin\mysqldump or whatever) and try again.

Marc B