Here is the code which reads and passes the dump file to the mysql server:
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);
echo "<div class='section'><h2 style='text-align:center;'>SQL commands executed</h2>";
foreach ($sqls as $sql) {
echo "<div style='border:solid 1px #888888;margin:0 auto 8px auto;width:80%;'>
<p style='text-align:left;'>".$sql."</p>";
if ($sql!=" ") {
if (!mysql_query($sql)) {
echo '<p style=\'color:red;\'>ERROR: '.mysql_error().'</p>';
} else {echo "<p style='color:red;'>SUCCES!</p>";
}
}
echo "</div>";
}
echo "</div>";
Bookmarks