-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax-show-table.php
165 lines (141 loc) · 3.92 KB
/
ajax-show-table.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
function removequote($input)
{
return str_replace(array('"', "'", "`"), "", $input);
}
if (isset($_POST)) {
foreach ($_POST as $key => $val) {
$_POST[$key] = removequote($val);
}
}
if (isset($_POST['db1']) && isset($_POST['db2'])) {
$host1 = (strlen(@$_POST['host1'])) ? (trim($_POST['host1'])) : 'localhost';
$port1 = (strlen(@$_POST['port1'])) ? (trim($_POST['port1'])) : 3306;
$db1 = trim(@$_POST['db1']);
$user1 = (strlen(@$_POST['user1'])) ? (trim($_POST['user1'])) : 'root';
$pass1 = (strlen(@$_POST['pass1'])) ? (trim($_POST['pass1'])) : '';
$host2 = (strlen(@$_POST['host2'])) ? (trim($_POST['host2'])) : 'localhost';
$port2 = (strlen(@$_POST['port2'])) ? (trim($_POST['port2'])) : 3306;
$db2 = trim(@$_POST['db2']);
$user2 = (strlen(@$_POST['user2'])) ? (trim($_POST['user2'])) : 'root';
$pass2 = (strlen(@$_POST['pass2'])) ? (trim($_POST['pass2'])) : '';
// test select db
try {
$tz = date('P');
$database1 = new PDO("mysql:host=" . $host1 . "; port=" . $port1 . "; dbname=" . $db1, $user1, $pass1);
$database1->exec("SET time_zone='$tz'");
$sdb1 = true;
} catch (PDOException $e) {
// do nothing
}
try {
$tz = date('P');
$database2 = new PDO("mysql:host=" . $host2 . "; port=" . $port2 . "; dbname=" . $db2, $user2, $pass2);
$database2->exec("SET time_zone='$tz'");
$sdb2 = true;
} catch (PDOException $e) {
// do nothing
}
if (!$sdb1 || !$sdb2) {
if (!$sdb1) {
$s1 = false;
} else {
$s1 = true;
}
if (!$sdb2) {
$s2 = false;
} else {
$s2 = true;
}
$output = array(
"db1" => array("connect" => true, "selectdb" => $s1, "data" => null),
"db2" => array("connect" => true, "selectdb" => $s2, "data" => null),
'diftbl' => null
);
echo json_encode($output);
exit();
}
$sql1 = "show table status";
try {
$ldb_rs = $database1->prepare($sql1);
$ldb_rs->execute();
if ($ldb_rs->rowCount()) {
$arr1 = $ldb_rs->fetchAll(PDO::FETCH_ASSOC);
}
} catch (Exception $e) {
// do nothing
}
$sql2 = "show table status";
try {
$ldb_rs = $database2->prepare($sql2);
$ldb_rs->execute();
if ($ldb_rs->rowCount()) {
$arr2 = $ldb_rs->fetchAll(PDO::FETCH_ASSOC);
}
} catch (Exception $e) {
// do nothing
}
// field list
$fields = array();
$fields['db1'] = array();
$fields['db2'] = array();
foreach ($arr1 as $key => $val) {
$table = $val['Name'];
$far = array();
$sql = "SHOW COLUMNS FROM `$table`";
try {
$ldb_rs = $database1->prepare($sql);
$ldb_rs->execute();
if ($ldb_rs->rowCount()) {
$result = $ldb_rs->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $dt) {
$far[] = $dt['Field'] . '|' . $dt['Type'] . '|' . $dt['Null'] . '|' . $dt['Key'] . '|' . $dt['Default'] . '|' . $dt['Extra'];
}
}
} catch (Exception $e) {
// do nothing
}
$fields['db1'][$table] = implode(",", $far);
}
foreach ($arr2 as $key => $val) {
$table = $val['Name'];
$far = array();
$sql = "SHOW COLUMNS FROM `$table`";
try {
$ldb_rs = $database2->prepare($sql);
$ldb_rs->execute();
if ($ldb_rs->rowCount()) {
$result = $ldb_rs->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $dt) {
$far[] = $dt['Field'] . '|' . $dt['Type'] . '|' . $dt['Null'] . '|' . $dt['Key'] . '|' . $dt['Default'] . '|' . $dt['Extra'];
}
}
} catch (Exception $e) {
// do nothing
}
$fields['db2'][$table] = implode(",", $far);
}
// list table which differ
$diftbl = array();
foreach ($fields['db1'] as $key => $val) {
if (isset($fields['db2'][$key])) {
if ($val != $fields['db2'][$key]) {
$diftbl[] = $key;
}
}
}
foreach ($fields['db2'] as $key => $val) {
if (isset($fields['db1'][$key])) {
if ($val != $fields['db1'][$key]) {
$diftbl[] = $key;
}
}
}
$diftbl = array_unique($diftbl);
$output = array(
"db1" => array("connect" => true, "selectdb" => true, "data" => $arr1),
"db2" => array("connect" => true, "selectdb" => true, "data" => $arr2),
'diftbl' => $diftbl
);
echo json_encode($output);
}