I have code as
$db_name = "db";
$outputfile = "/somewhere";
$new_db_name = 'newdb';
$cmd = 'mysqldump --skip-triggers %s > %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
if ($ret !=0 ) {
//log error message in $output
}
Then to import:
$cmd = 'mysql --database=%s < %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($new_db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
//etc.
unlink($outputfile);
But here what should i do to get the export query, rather than creating a file everytime?
EDITED:
IN THE RESPONSE OF REPLY OF MATT
- I am using Windows, will the code u have given work as I am not sure the code I already have will work in Windows as it seems to be linux commands?
- Its my need to script in PHP its the process which is going to be triggered when installing a component in joomla
- What is PIPE in PHP?
Do you have to do this in PHP? You can easily do this in a terminal with some basic piping (no need to create any files).
Two commands and you’re done:
This copies
foointo new databasefoo2Note that it is possible to pipe in php .. but .. really, you should just do this in shell (IMO).
Edit:
Oops, for some reason I thought you wanted triggers. Edited above to include
--skip-triggersEdit 2:
‘Piping’ refers to routing
stdoutandstdin.Windows supports pipes. The above commands are working for me on Windows.
However, I am not able to get piping to work through PHP with Windows (at least not for
mysql). It’s like the streams fromproc_openignore my input.So the alternative would be to take the commands I provided above, put them in a
.batfile, and see if you can call that script viasystem()Final edit:
You’re going to need to do some research as I can’t explain every small detail. Did you reference the
system()documentation I pointed to? It lets you call system commands ..My suggestion is you make a basic script, call it
bla.bat:If that’s in the same folder as your script, then your script can do this:
Viola. DB copied.