php dbAcces array require config php new_data_base _SERVER argv resDb

 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
37
38
<?php
$dbAcces = array();
require 'config.php';
$new_data_base = $_SERVER['argv'][1];
$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";
$sql = "SHOW TABLES FROM ".$dbAcces['dbName'];
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
mysql_select_db($dbAcces['dbName']);
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 `'.$dbAcces['dbName'].'`.`'.$row[0].'`;');
$dataresult = mysql_query('SELECT * FROM `'.$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);