{"id":339,"date":"2011-10-21T21:36:32","date_gmt":"2011-10-21T13:36:32","guid":{"rendered":"http:\/\/www.zengz.com\/?p=339"},"modified":"2011-10-21T21:39:21","modified_gmt":"2011-10-21T13:39:21","slug":"%e5%ae%8c%e6%88%90%e6%83%b3%e4%ba%86%e4%b8%80%e5%b9%b4%e5%a4%9a%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.zengz.com\/?p=339","title":{"rendered":"\u5b8c\u6210\u60f3\u4e86\u4e00\u5e74\u591a\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u4e4b\u524d\u8fd0\u884c\u5728mysql4.1.22\u7684\u7f51\u7ad9\uff0c\u7a0b\u5e8f\u7f16\u7801\u662fgb2312\uff0c\u6570\u636e\u5e93\u662futf8\uff0c\u4e8e\u662f\u5e93\u91cc\u7684\u5185\u5bb9\u5b8c\u5168\u4e71\u7801\u3002\u4f7f\u7528\u5404\u79cd\u5de5\u5177\u5bfc\u51fa\uff0cphpmyadmin\u4fee\u6539\u5404\u79cd\u7f16\u7801\u90fd\u65e0\u6548\u3002\u7ec8\u4e8e\u6700\u8fd1\u5fc5\u987b\u8fc1\u79fb\u5230mysql5\u627e\u51fa\u4e86\u529e\u6cd5\u3002<\/p>\n<p>\u539f\u73af\u5883\uff1a<\/p>\n<p>Apache1.3.41<br \/>\nPHP4.4.9<br \/>\nMysql4.1.22 \u5b57\u7b26\u96c6utf8\uff0c\u8fde\u63a5\u6821\u5bf9utf8_general_ci\uff0c\u6570\u636e\u5e93 latin1\uff0c\u6570\u636e\u8868latin1<br \/>\n\u7f51\u7ad9 gb2312<\/p>\n<p>\u65b0\u73af\u5883<\/p>\n<p>Apache2.2.21 PHP5.3.8 Mysql5.5.16<!--more--><\/p>\n<h3>1.\u6570\u636e\u5bfc\u51fa<\/h3>\n<p>\u6839\u636e\u4e00\u4e9b\u6587\u7ae0\u7684\u8bf4\u6cd5\uff0c\u4f7f\u7528mysqldump \u5404\u79cd set names\u65e0\u6548\uff1b\u4f7f\u7528Mysql GUI Tools\u65e0\u6548\u3002\u5bfc\u51fa\u540e\u7684sql\u6587\u4ef6\u4e2d\u6587\u4e71\u7801\u3002\u8003\u8651\u4e00\u4e2a\u601d\u8def\uff0c\u65e2\u7136gb2312\u7684\u7f51\u7ad9\u80fd\u591f\u6b63\u5e38\u663e\u793a\uff0c\u90a3\u4e48\u4f7f\u7528\u7f51\u7ad9\u4ee3\u7801\u8fdb\u884c\u5e93\u7684\u5bfc\u51fa\u5c31\u4e0d\u4f1a\u4e71\u7801\u3002<\/p>\n<p>\u53c2\u8003\u6b64\u6587http:\/\/huangdekai1.blog.163.com\/blog\/static\/3057141620114110716677\/\uff0c\u56e0\u4e3a\u4e0d\u60f3\u7528\u547d\u4ee4\u884c\uff0c\u6240\u4ee5\u7565\u52a0\u4fee\u6539<\/p>\n<p style=\"padding-left: 30px;\">&lt;?php<\/p>\n<p style=\"padding-left: 30px;\">$database = &#8220;&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">$options = array(<\/p>\n<p style=\"padding-left: 30px;\">&#8216;hostname&#8217; =&gt; &#8216;localhost&#8217;,<\/p>\n<p style=\"padding-left: 30px;\">&#8216;charset&#8217; =&gt; &#8221;,<\/p>\n<p style=\"padding-left: 30px;\">&#8216;filename&#8217; =&gt; &#8216;xx.sql&#8217;,<\/p>\n<p style=\"padding-left: 30px;\">&#8216;username&#8217; =&gt; &#8221;,<\/p>\n<p style=\"padding-left: 30px;\">);<\/p>\n<p style=\"padding-left: 30px;\">$options[&#8216;password&#8217;] = &#8220;&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">mysql_connect($options[&#8216;hostname&#8217;], $options[&#8216;username&#8217;], $options[&#8216;password&#8217;]);<\/p>\n<p style=\"padding-left: 30px;\">mysql_select_db($database);<\/p>\n<p style=\"padding-left: 30px;\">\/\/mysql_query(&#8220;SET NAMES &#8216;{$options[&#8216;charset&#8217;]}'&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">\/\/ \u8bbe\u7f6e\u8981\u5bfc\u51fa\u7684\u8868<\/p>\n<p style=\"padding-left: 30px;\">$tables = list_tables($database);<\/p>\n<p style=\"padding-left: 30px;\">$filename = sprintf($options[&#8216;filename&#8217;], $database);<\/p>\n<p style=\"padding-left: 30px;\">$fp = fopen($filename, &#8216;w&#8217;);<\/p>\n<p style=\"padding-left: 30px;\">foreach ($tables as $table) {<\/p>\n<p style=\"padding-left: 30px;\">dump_table($table, $fp);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">fclose($fp);<\/p>\n<p style=\"padding-left: 30px;\">mysql_close();<\/p>\n<p style=\"padding-left: 30px;\">echo &#8220;done.\\n&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">exit;<\/p>\n<p style=\"padding-left: 30px;\">function list_tables($database)<\/p>\n<p style=\"padding-left: 30px;\">{<\/p>\n<p style=\"padding-left: 30px;\">$rs = mysql_list_tables($database);<\/p>\n<p style=\"padding-left: 30px;\">$tables = array();<\/p>\n<p style=\"padding-left: 30px;\">while ($row = mysql_fetch_row($rs)) {<\/p>\n<p style=\"padding-left: 30px;\">$tables[] = $row[0];<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">mysql_free_result($rs);<\/p>\n<p style=\"padding-left: 30px;\">return $tables;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">function dump_table($table, $fp = null)<\/p>\n<p style=\"padding-left: 30px;\">{<\/p>\n<p style=\"padding-left: 30px;\">$need_close = false;<\/p>\n<p style=\"padding-left: 30px;\">if (is_null($fp)) {<\/p>\n<p style=\"padding-left: 30px;\">$fp = fopen($table . &#8216;.sql&#8217;, &#8216;w&#8217;);<\/p>\n<p style=\"padding-left: 30px;\">$need_close = true;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">fwrite($fp, &#8220;&#8211; \\n&#8211; {$table}\\n&#8211; \\n&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">$rs = mysql_query(&#8220;SELECT * FROM `{$table}`&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">while ($row = mysql_fetch_row($rs)) {<\/p>\n<p style=\"padding-left: 30px;\">fwrite($fp, get_insert_sql($table, $row));<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">mysql_free_result($rs);<\/p>\n<p style=\"padding-left: 30px;\">if ($need_close) {<\/p>\n<p style=\"padding-left: 30px;\">fclose($fp);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">fwrite($fp, &#8220;\\n\\n&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">function get_insert_sql($table, $row)<\/p>\n<p style=\"padding-left: 30px;\">{<\/p>\n<p style=\"padding-left: 30px;\">$sql = &#8220;INSERT INTO `{$table}` VALUES (&#8220;;<\/p>\n<p style=\"padding-left: 30px;\">$values = array();<\/p>\n<p style=\"padding-left: 30px;\">foreach ($row as $value) {<\/p>\n<p style=\"padding-left: 30px;\">$values[] = &#8220;&#8216;&#8221; . mysql_real_escape_string($value) . &#8220;&#8216;&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">$sql .= implode(&#8216;, &#8216;, $values) . &#8220;);\\n&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">return $sql;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">?&gt;<\/p>\n<p>\u6ce8\u610f\u8fd9\u884c\u4ee3\u7801<\/p>\n<p>\/\/mysql_query(&#8220;SET NAMES &#8216;{$options[&#8216;charset&#8217;]}'&#8221;);<\/p>\n<p>\u8fd9\u91cc\u8bbe\u7f6e\u4e86gbk\u548cutf8\u90fd\u4f1a\u5bfc\u81f4\u4e71\u7801\uff0c\u6ce8\u91ca\u6389\u53cd\u800c\u80fd\u6b63\u5e38\u5bfc\u51fa\u4e2d\u6587\u3002\u5bfc\u51fa\u540e\u4f7f\u7528editplus\u53e6\u5b58\u4e3autf8\u683c\u5f0f\u3002<\/p>\n<h3>2.\u5efa\u7acb\u6570\u636e\u5e93<\/h3>\n<p>\u7531\u4e8e\u4e0a\u9762\u5bfc\u51fa\u7684\u53ea\u6709insert\u8bed\u53e5\u7684\u6570\u636e\uff0c\u4f7f\u7528phpmyadmin\u5c06\u539f\u7ed3\u6784\u5bfc\u51fa\uff0c\u4fee\u6539\u5bfc\u51fa\u540e\u7684sql\u4e2d\u7684latin1\u4e3autf8\uff0c\u6267\u884csql\u65b0\u5efa\u5e93\u3002\u6b64\u65f6\u5e93\u548c\u8868\u7f16\u7801\u90fd\u662futf8\u4e86\u3002<\/p>\n<h3>3.\u5bfc\u5165\u6570\u636e<\/h3>\n<p>\u7b2c1\u6b65\u5bfc\u51fa\u7684\u6570\u636e\u53ef\u80fd\u5f88\u5927\u3002\u6240\u4ee5\u4f7f\u7528Mysql Gui Tools\u5bfc\u5165\u3002\u8fd9\u91cc\u76f4\u63a5\u7528\u201c\u6062\u590d\u201d\u662f\u4e0d\u884c\u7684\uff0c\u8981\u7528Tools\u91cc\u9762\u7684Mysql\u67e5\u8be2\u6d4f\u89c8\u5668\uff0c\u6587\u4ef6-&gt;\u6253\u5f00\u811a\u672c\u3002\u6267\u884c\u4e2d\u5982\u679c\u6709\u54ea\u4e2ainsert\u8bed\u53e5\u51fa\u9519\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u7a97\u53e3\u76f4\u63a5\u4fee\u6539\u540e\u6267\u884c\u3002<\/p>\n<h3>4.\u7a0b\u5e8f\u683c\u5f0f\u8f6c\u6362<\/h3>\n<p>\u7ecf\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6570\u636e\u5c31\u8f6c\u6362\u4e3autf8\u683c\u5f0f\u3002\u4f46\u662f\u7f51\u7ad9\u7a0b\u5e8f\u4f9d\u7136\u662fgb2312\uff0c\u6b64\u65f6\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\u7528gb2utf8\u5c06\u6240\u6709html\u548cphp\u6587\u4ef6\u8f6c\u4e3autf8\uff0c\u6ce8\u610f\u8981\u628a\u5df2\u7ecf\u662futf8\u7684\u6587\u4ef6\u6392\u9664\uff0c\u5426\u5219\u8f6c\u6362\u540e\u4f1a\u4ea7\u751f\u4e71\u7801\uff1b\u7136\u540e\u7528search&amp;replace\u5c06\u6240\u6709header\u91cc\u9762\u7684chartset\u6539\u4e3autf8.<\/p>\n<p>\u8f6c\u6362\u5168\u90e8\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u8fd0\u884c\u5728mysql4.1.22\u7684\u7f51\u7ad9\uff0c\u7a0b\u5e8f\u7f16\u7801\u662fgb2312\uff0c\u6570\u636e\u5e93\u662futf8\uff0c\u4e8e\u662f\u5e93\u91cc\u7684\u5185\u5bb9\u5b8c\u5168\u4e71\u7801\u3002\u4f7f\u7528 &hellip; <a href=\"http:\/\/www.zengz.com\/?p=339\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5b8c\u6210\u60f3\u4e86\u4e00\u5e74\u591a\u7684\u95ee\u9898\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-339","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339"}],"version-history":[{"count":2,"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":341,"href":"http:\/\/www.zengz.com\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions\/341"}],"wp:attachment":[{"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zengz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}