php dbAcces array require config php old_data_base _SERVER argv new_da

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
$dbAcces = array();
require 'config.php';
$old_data_base = $_SERVER['argv'][1];
$new_data_base = $_SERVER['argv'][2];
$resDb = mysql_connect(
'localhost:'.$dbAcces['sqlPort'],
$dbAcces['sqlRoot'],
$dbAcces['sqlPasswd']
);
if(!$resDb){
echo "Error: Connect to database. Refer to administrator.\n";
exit;
}
echo "Success: Connect to database.\n";
$result = mysql_query('SHOW TABLES FROM `'.$dbAcces['dbName'].'`');
if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit;}
mysql_query('DROP DATABASE IF EXISTS `'.$new_data_base.'`');
mysql_query('CREATE DATABASE `'.$new_data_base.'`;');
echo mysql_error().PHP_EOL;
while ($row = mysql_fetch_row($result)) {
echo 'Copy table '.$row[0].PHP_EOL;
mysql_query('CREATE TABLE `'.$new_data_base.'`.`'.$row[0].'` LIKE `'.$old_data_base.'`.`'.$row[0].'`;');
$dataresult = mysql_query('SELECT * FROM `'.$old_data_base.'`.`'.$row[0].'`;');
while($dataRow = mysql_fetch_assoc($dataresult)) {
mysql_query('INSERT INTO `'.$new_data_base.'`.`'.$row[0].'` (`'.implode('`, `', array_keys($dataRow)).'`)VALUES ("'.implode('", "', array_map('mysql_real_escape_string', $dataRow)).'")');
}
mysql_free_result($dataresult);
}
mysql_free_result($result);