dbcompare.php
Code (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
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
<?
ini_set('display_erros',1);
error_reporting(E_ALL);
$dbcredentials[1] = array(
'host' => '',
'dbusername' => '',
'dbpassword' => '',
'dbname' => ''
);
$dbcredentials[2] = array(
'host' => '',
'dbusername' => '',
'dbpassword' => '',
'dbname' => ''
);
# Verzamel de gegevens
foreach($dbcredentials as $i => $db){
$conn[$i] = mysql_connect($db['host'], $db['dbusername'], $db['dbpassword']);
if(!mysql_select_db($db['dbname'],$conn[$i])) {
die('Kan geen verbinding maken met de database '.$db['dbname']);
}
# Verzamel tabellen
$qry = mysql_query("SHOW TABLES",$conn[$i]);
while($info = mysql_fetch_assoc($qry)){
$dbtables[$i][] = reset($info);
}
# Verzamel kolommen van tabellen
foreach($dbtables[$i] as $table){
$qry = mysql_query("SHOW COLUMNS FROM ".$table, $conn[$i]);
while($info = mysql_fetch_assoc($qry)){
$tables_with_fields[$i][$table][$info['Field']] = $info['Type'];
}
}
}
?>
<h1>DataBase Vergelijker</h1>
<?
# Houdt bij, welke databases al met elkaar zijn vergeleken
$done = array();
# Vergelijk de gegevens
foreach($dbcredentials as $i => $db_master){
if(!isset($done[$i])){
$done[$i] = array();
}
foreach($dbcredentials as $j => $db_slave){
if(!isset($done[$j])){
$done[$j] = array();
}
# Zorg dat je een tabel vergelijking, maar 1 keer uitvoert.
if($i != $j && !in_array($j, $done[$i]) && !in_array($i,$done[$j])){
$done[$i][] = $j;
$done[$j][] = $i;
?>
<table width="100%">
<tr>
<td width="50%" valign="top">
<h2><?=$db_master['dbname'].'@'.$db_master['host']?> => <?=$db_slave['dbname'].'@'.$db_slave['host']?></h2>
<pre><?
foreach($tables_with_fields[$i] as $table => $fields){
if(array_key_exists($table,$tables_with_fields[$j])){
print '<span style="color: green;">Table <strong>'.$table.'</strong> bestaat in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
foreach($fields as $field => $type){
if(!array_key_exists($field,$tables_with_fields[$j][$table])){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> bestaat niet in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}else{
if($type != $tables_with_fields[$j][$table][$field]){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> is niet hetzelfde veldtype als in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
}
}
}else{
print '<span style="color: red;">Table <strong>'.$table.'</strong> bestaat niet in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
print '-------------------------------------------------------'."\n";
}
?></pre>
</td>
<td valign="top">
<h2><?=$db_slave['dbname'].'@'.$db_slave['host']?> => <?=$db_master['dbname'].'@'.$db_master['host']?></h2>
<pre><?
foreach($tables_with_fields[$j] as $table => $fields){
if(array_key_exists($table,$tables_with_fields[$i])){
print '<span style="color: green;">Table <strong>'.$table.'</strong> bestaat in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
foreach($fields as $field => $type){
if(!array_key_exists($field,$tables_with_fields[$i][$table])){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> bestaat niet in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
}else{
if($type != $tables_with_fields[$i][$table][$field]){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> is niet hetzelfde veldtype als in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
}
}
}else{
print '<span style="color: red;">Table <strong>'.$table.'</strong> bestaat niet in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
}
print '-------------------------------------------------------'."\n";
}
?></pre>
</td>
</tr>
</table>
<?
}
}
}
?>
ini_set('display_erros',1);
error_reporting(E_ALL);
$dbcredentials[1] = array(
'host' => '',
'dbusername' => '',
'dbpassword' => '',
'dbname' => ''
);
$dbcredentials[2] = array(
'host' => '',
'dbusername' => '',
'dbpassword' => '',
'dbname' => ''
);
# Verzamel de gegevens
foreach($dbcredentials as $i => $db){
$conn[$i] = mysql_connect($db['host'], $db['dbusername'], $db['dbpassword']);
if(!mysql_select_db($db['dbname'],$conn[$i])) {
die('Kan geen verbinding maken met de database '.$db['dbname']);
}
# Verzamel tabellen
$qry = mysql_query("SHOW TABLES",$conn[$i]);
while($info = mysql_fetch_assoc($qry)){
$dbtables[$i][] = reset($info);
}
# Verzamel kolommen van tabellen
foreach($dbtables[$i] as $table){
$qry = mysql_query("SHOW COLUMNS FROM ".$table, $conn[$i]);
while($info = mysql_fetch_assoc($qry)){
$tables_with_fields[$i][$table][$info['Field']] = $info['Type'];
}
}
}
?>
<h1>DataBase Vergelijker</h1>
<?
# Houdt bij, welke databases al met elkaar zijn vergeleken
$done = array();
# Vergelijk de gegevens
foreach($dbcredentials as $i => $db_master){
if(!isset($done[$i])){
$done[$i] = array();
}
foreach($dbcredentials as $j => $db_slave){
if(!isset($done[$j])){
$done[$j] = array();
}
# Zorg dat je een tabel vergelijking, maar 1 keer uitvoert.
if($i != $j && !in_array($j, $done[$i]) && !in_array($i,$done[$j])){
$done[$i][] = $j;
$done[$j][] = $i;
?>
<table width="100%">
<tr>
<td width="50%" valign="top">
<h2><?=$db_master['dbname'].'@'.$db_master['host']?> => <?=$db_slave['dbname'].'@'.$db_slave['host']?></h2>
<pre><?
foreach($tables_with_fields[$i] as $table => $fields){
if(array_key_exists($table,$tables_with_fields[$j])){
print '<span style="color: green;">Table <strong>'.$table.'</strong> bestaat in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
foreach($fields as $field => $type){
if(!array_key_exists($field,$tables_with_fields[$j][$table])){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> bestaat niet in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}else{
if($type != $tables_with_fields[$j][$table][$field]){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> is niet hetzelfde veldtype als in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
}
}
}else{
print '<span style="color: red;">Table <strong>'.$table.'</strong> bestaat niet in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
print '-------------------------------------------------------'."\n";
}
?></pre>
</td>
<td valign="top">
<h2><?=$db_slave['dbname'].'@'.$db_slave['host']?> => <?=$db_master['dbname'].'@'.$db_master['host']?></h2>
<pre><?
foreach($tables_with_fields[$j] as $table => $fields){
if(array_key_exists($table,$tables_with_fields[$i])){
print '<span style="color: green;">Table <strong>'.$table.'</strong> bestaat in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
foreach($fields as $field => $type){
if(!array_key_exists($field,$tables_with_fields[$i][$table])){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> bestaat niet in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
}else{
if($type != $tables_with_fields[$i][$table][$field]){
print "\t".'<span style="color: red;">Veld <strong>'.$field.'</strong> is niet hetzelfde veldtype als in '.$db_slave['dbname'].'@'.$db_slave['host'].'</span>'."\n";
}
}
}
}else{
print '<span style="color: red;">Table <strong>'.$table.'</strong> bestaat niet in '.$db_master['dbname'].'@'.$db_master['host'].'</span>'."\n";
}
print '-------------------------------------------------------'."\n";
}
?></pre>
</td>
</tr>
</table>
<?
}
}
}
?>