Someone suggested to explode() the dump text by ";" into an array and run each command separately with mysql_query(). That nearly works, except that ";" also occurs in the database entries, e.g.:
INSERT IGNORE INTO 'table' VALUES (1,2,'this is a text; it has a semi-colon in it',3,4);
I found a work-around which works:
PHP Code:
$file = "dump.sql";
$fh = fopen($file, 'r');
$text = fread($fh, filesize($file));
fclose($fh);
// insert seperator before each command
$sep=" |SEP| ";
$text=ereg_replace("DROP"," $sep DROP",$text);
$text=ereg_replace("INSERT"," $sep INSERT",$text);
$text=ereg_replace("CREATE"," $sep CREATE",$text);
$text=ereg_replace("--"," $sep --",$text);
$text = eregi_replace(
'/\*([^\\[]+)\*/',
'', $text); // remove what is btw /* and */
// create array from seperator and run
// queries one-by-one
$sqls=explode(' |SEP| ',$text);
foreach ($sqls as $sql) {
echo '<div style=\'border:solid 1px grey;margin:0 auto 8px auto;width:500px;\'><p>'
.$sql.'</p>';
if (!mysql_query($sql)) {
echo '<p style=\'color:red;\'>ERROR: '.mysql_error().'</p>';
} else {echo "<p style='color:red;'>SUCCES!</p>";
}
echo "</div>";
}
- as long as someone doesn't write content in the database with the words INSERT, CREATE or DROP in capital letters - or content which is /*whithin these things*/. So it's not entirely fool-proof.
There must be some way of running the full dump in one go ...
Bookmarks