0

Certainly o could generate a dump file, then read this file and put into a variable. But is there a way to retrieve the dump and put into the variable directly? Thanks!

ABA
  • 435
  • 1
  • 5
  • 13
  • Yes, it's possible. But nobody's going to spend an hour writing the complete code for you, not at least anyone who knows what he's doing. That's not what this site is for. – Álvaro González Apr 29 '13 at 09:35
  • @alvaro, you do this in 1 line... wouldn't take me an hour to write ;) – nvanesch Apr 29 '13 at 09:39
  • @nvanesch That's because you are assuming he already knows how to create a dump from PHP and he'll understand your line of code and tweak it to his need—I wouldn't count on either things. – Álvaro González Apr 29 '13 at 09:43
  • first line het tells us he can generate it, read the file and output it. – nvanesch Apr 29 '13 at 09:46

2 Answers2

0

you can run it with shell_exec.

then all output is put in the php variable.

http://nl3.php.net/shell_exec

Edit, but please note that the output buffer for shell_exec is not unlimited. So if the DB grows very large, you will reach a moment where it will simply not function anymore. Also putting this in a variable puts it in memory. This might take up a lot of memory if you have a large DB.

I would advise to write it to a file and throughput it directly to the output when needed in php.

nvanesch
  • 2,540
  • 14
  • 25
0

You can use shell_exec() or exec() but puting all output to variable is bad idea.

exec('mysqldump --user=your_user --password=your_pass --host=your_host DATABASENAME > /path/dump.sql');

you can use exec and after it finishes work open file with file_get_contents()

more on:

http://pl1.php.net/exec

http://pl1.php.net/manual/en/function.exec.php

http://pl1.php.net/manual/en/function.shell-exec.php

mysqldump is a program that should be in your BIN directory.

Robert
  • 19,800
  • 5
  • 55
  • 85