Created
May 1, 2017 10:34
-
-
Save kousherAlam/d73ade5b646fe3972f9a80565a0855fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This function is reponsible for rest the database. | |
* take two parameter first is array for connection to the database | |
* It will like ["hostname","dbuser","dbpass","dbname"] | |
* Secound argument for the reset db file location. | |
* first establish the connection , and then tranclate all table, | |
* Then read the content form the database file, | |
* insert all db file content using multiquery | |
@ return array [TRUE/FALSE,'Description String'] | |
*/ | |
function reset_database($access,$resetfile){ | |
try { | |
$conn = new mysqli($access[0], $access[1], $access[2], $access[3]); | |
if ($conn->connect_error) { | |
return [FALSE,'Opps DB Error!!']; | |
} | |
$result = $conn->query("SHOW TABLES"); | |
while($row = $result->fetch_assoc()){ | |
foreach($row as $v){ | |
if($v != null && !empty($v)){ | |
if(!($conn->query("TRUNCATE TABLE {$v}"))){ | |
return [FALSE,'Table Tranclation Failed']; | |
} | |
} | |
} | |
} | |
$data_is = file_get_contents($resetfile); | |
if (!$conn->multi_query($data_is)) { | |
return [FALSE,'Data Insertion Failed']; | |
} | |
$conn->close(); | |
return [TRUE,'Data Reset Successfull']; | |
} | |
catch (Exception $e) { | |
return [FALSE,'Exception Error']; | |
} | |
} | |
/* | |
* This function is reponsible for reseting multiple database. | |
* it take an multidimention array with all of the database information | |
* which need to reset. and run the reset_database function for every array. | |
@ return multidimenstion array , description of what's going on. | |
*/ | |
function reset_multiple_database($database_to_reset){ | |
$result = array( | |
'success' => array(), | |
'fail' => array(); | |
); | |
foreach($database_to_reset as $value){ | |
$reset_result = reset_database($value['connection'],$value['dbfile']); | |
if($reset_result[0]){ | |
$str = "[Database ".$value['connection'][3]."] :- {$reset_result[1]}"; | |
array_push($result['success'], $str); | |
}else{ | |
$str = "[Database ".$value['connection'][3]."] :-{$reset_result[1]}"; | |
array_push($result['fail'], $str); | |
} | |
} | |
return $result; | |
} | |
/*/ uses: Uncomment for test | |
$database_to_reset = [ | |
array( | |
'connection' => ['hostname', 'dbuser','dbpass','dbname'], | |
'dbfile' => 'fileForReset' | |
), | |
]; | |
reset_multiple_database($database_to_reset); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment