mysqldump-php
mysqldump-php copied to clipboard
Provide a restore method
Provide a restore method, please?
What this method can do?
something like this: (minus the bugs people will surely spot given enough eyeballs)
$db = new PDO($db_dsn, $db_user, $db_pass, $db_options);
// Temporary variable, used to store current query
$templine = '';
$handle = fopen($dump_file , 'r');
if ($handle) {
while (!feof($handle)) { // Loop through each line
$line = trim(fgets($handle));
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
// Skip it if it's a DEFINER
// if (strpos($line, 'DEFINER') !== false) {
// $line = '';
// }
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
$db->query($templine);
// Reset temp variable to empty
$templine = '';
}
}
fclose($handle);
}
https://github.com/ifsnop/mysqldump-php/wiki/Importing-dump-from-php
yup, add that code to the class as a restore method
The example uses mysqli, if you transform it to use PDO I could certainly include it.
yup, add that to the class as a restore method
The example uses mysqli, if you transform it to use PDO I could certainly
my posted code (see 4 comments above) uses PDO...
(and I'm using that code in production, but I think it belongs to this class)
To be honest, I'm not a fan of all the examples provided, because:
- There's no memory control. Whole query is being loaded into memory, even if it's a huge comma separated 300mb table values dump.
- There's no error control. What happens if 1234th query returns execution error (for example, table already exists, or have duplicate keys)?
- It's slow. For tiny databases this can be OK, but on large scale this approach can't compete with simple streaming dump file to mysql.
If we talk about proper importing function built into mysqldump-php, we should not go with a simple slow solution, it sould be a solid mysqlimport competitor. Otherwise it's not worth it.
Hi Volkov,
- No. The file is opened and read line by line.
- Feel free to post an enhanced version, but today people are using this code (this is one of the advantages of having this code in the library, there are more people reviewing it and able to post patches)
- No one is forcing you to use it, you can choose any other method, don't worry.
I think it should start simple, and growing from there...
I merged the proposed solution, since not everyone has mysql client to be able to import in a shared hosting.