-
Notifications
You must be signed in to change notification settings - Fork 185
/
my-sql-import (restore) database.php
21 lines (20 loc) · 1.8 KB
/
my-sql-import (restore) database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// EXAMPLE: IMPORT_TABLES("localhost","user","pass","db_name", "my_baseeee.sql"); //TABLES WILL BE OVERWRITTEN
// P.S. IMPORTANT NOTE for people who try to change/replace some strings in SQL FILE before importing, MUST READ: https://github.com/ttodua/useful-php-scripts/blob/master/my-sql-export%20(backup)%20database.php
// https://github.com/ttodua/useful-php-scripts
function IMPORT_TABLES($host,$user,$pass,$dbname, $sql_file_OR_content){
set_time_limit(3000);
$SQL_CONTENT = (strlen($sql_file_OR_content) > 300 ? $sql_file_OR_content : file_get_contents($sql_file_OR_content) );
$allLines = explode("\n",$SQL_CONTENT);
$mysqli = new mysqli($host, $user, $pass, $dbname); if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$zzzzzz = $mysqli->query('SET foreign_key_checks = 0'); preg_match_all("/\nCREATE TABLE(.*?)\`(.*?)\`/si", "\n". $SQL_CONTENT, $target_tables); foreach ($target_tables[2] as $table){$mysqli->query('DROP TABLE IF EXISTS '.$table);} $zzzzzz = $mysqli->query('SET foreign_key_checks = 1'); $mysqli->query("SET NAMES 'utf8'");
$templine = ''; // Temporary variable, used to store current query
foreach ($allLines as $line) { // Loop through each line
if (substr($line, 0, 2) != '--' && $line != '') {$templine .= $line; // (if it is not a comment..) Add this line to the current segment
if (substr(trim($line), -1, 1) == ';') { // If it has a semicolon at the end, it's the end of the query
if(!$mysqli->query($templine)){ print('Error performing query \'<strong>' . $templine . '\': ' . $mysqli->error . '<br /><br />'); } $templine = ''; // set variable to empty, to start picking up the lines after ";"
}
}
} return 'Importing finished. Now, Delete the import file.';
} //see also export.php
?>