这天遇见个需求:由于升级系统 这些系统从A库升级到B库,但是不知道数据库添加了哪些字段和增加了哪些表。当然可以去PHPMYADMIN里浏览一下。但是终究有遗漏吧。所以自己写了个,比较数据库差异的脚本(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
<span style="color: #000000;font-weight: bold"><?</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_connect" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_connect</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'localhost'</span><span style="color: #339933">,</span><span style="color: #0000ff">'root'</span><span style="color: #339933">,</span><span style="color: #0000ff">'root'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_select_db" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_select_db</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'tablea'</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//标准的数据库</span>
<span style="color: #000088">$q</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"show tables"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">while</span><span style="color: #009900">(</span><span style="color: #000088">$s</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$q</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #000088">$name</span> <span style="color: #339933">=</span> <span style="color: #000088">$s</span><span style="color: #009900">[</span><span style="color: #cc66cc">0</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
	<span style="color: #000088">$q1</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"desc <span style="color: #006699;font-weight: bold">$name</span>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">while</span> <span style="color: #009900">(</span><span style="color: #000088">$s1</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$q1</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$a</span><span style="color: #009900">[</span><span style="color: #000088">$name</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #009900">]</span> <span style="color: #339933">=</span><span style="color: #000088">$s1</span><span style="color: #009900">[</span><span style="color: #cc66cc">0</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_close" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_close</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_connect" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_connect</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'localhost'</span><span style="color: #339933">,</span><span style="color: #0000ff">'root'</span><span style="color: #339933">,</span><span style="color: #0000ff">'root'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_select_db" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_select_db</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'tableb'</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//需要比较的数据库</span>
<span style="color: #000088">$q2</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"show tables"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">while</span><span style="color: #009900">(</span><span style="color: #000088">$s2</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$q2</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #000088">$name2</span><span style="color: #339933">=</span> <span style="color: #000088">$s2</span><span style="color: #009900">[</span><span style="color: #cc66cc">0</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
	<span style="color: #000088">$q3</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"desc <span style="color: #006699;font-weight: bold">$name2</span>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">while</span> <span style="color: #009900">(</span><span style="color: #000088">$s3</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$q3</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$aa</span><span style="color: #009900">[</span><span style="color: #000088">$name2</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #009900">]</span> <span style="color: #339933">=</span><span style="color: #000088">$s3</span><span style="color: #009900">[</span><span style="color: #cc66cc">0</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
<span style="color: #009900">}</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_close" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_close</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$f</span> <span style="color: #339933">=</span> <span style="color: #000088">$e</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$str</span> <span style="color: #339933">=</span> <span style="color: #000088">$fuhao</span> <span style="color: #339933">=</span><span style="color: #0000ff">''</span><span style="color: #339933">;</span>
<span style="color: #b1b100">foreach</span><span style="color: #009900">(</span><span style="color: #000088">$a</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$k</span><span style="color: #339933">=></span><span style="color: #000088">$v</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$aa</span><span style="color: #009900">[</span><span style="color: #000088">$k</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$e</span><span style="color: #009900">[</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #000088">$k</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<span style="color: #b1b100">else</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">if</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/count" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">count</span></a><span style="color: #009900">(</span><span style="color: #000088">$aa</span><span style="color: #009900">[</span><span style="color: #000088">$k</span><span style="color: #009900">]</span><span style="color: #009900">)</span> <span style="color: #339933"><></span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/count" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">count</span></a><span style="color: #009900">(</span><span style="color: #000088">$v</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>	
			<span style="color: #b1b100">foreach</span><span style="color: #009900">(</span><span style="color: #000088">$v</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$k1</span><span style="color: #339933">=></span><span style="color: #000088">$v1</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
				<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/in_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">in_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$v1</span><span style="color: #339933">,</span><span style="color: #000088">$aa</span><span style="color: #009900">[</span><span style="color: #000088">$k</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
					<span style="color: #000088">$f</span><span style="color: #009900">[</span><span style="color: #000088">$k</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #000088">$v1</span><span style="color: #339933">;</span>
				<span style="color: #009900">}</span>
			<span style="color: #009900">}</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"<pre>"</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$e</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缺少表</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$f</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缺少表的字段</span>
 
<span style="color: #000000;font-weight: bold">?></span>