{"id":238,"date":"2009-02-28T10:09:00","date_gmt":"2009-02-28T02:09:00","guid":{"rendered":""},"modified":"2013-11-29T14:17:11","modified_gmt":"2013-11-29T06:17:11","slug":"mysqlphp%e4%b8%ad%e6%96%87%e6%98%be%e7%a4%ba%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/238.html","title":{"rendered":"mysql+php\u4e2d\u6587\u663e\u793a\u4e71\u7801\u7684\u89e3\u51b3"},"content":{"rendered":"<p>\u5efa\u8bae\u6570\u636e\u5e93\u7528utf8\u7f16\u7801<\/p>\n<p>\u95ee\u9898\u6c47\u603b\uff1a<\/p>\n<p>1.mysql\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u7f16\u7801\u662futf8,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684PHP\u7f51\u9875\u4e0d\u4e00\u81f4,\u53ef\u80fd\u5c31\u4f1a\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>2.MYSQL\u4e2d\u521b\u5efa\u8868\u65f6\u4f1a\u8ba9\u4f60\u9009\u62e9\u4e00\u79cd\u7f16\u7801,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684\u7f51\u9875\u7f16\u7801\u4e0d\u4e00\u81f4,\u4e5f\u53ef\u80fd\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>3.MYSQL\u521b\u5efa\u8868\u65f6\u6dfb\u52a0\u5b57\u6bb5\u662f\u53ef\u4ee5\u9009\u62e9\u7f16\u7801\u7684,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684\u7f51\u9875\u7f16\u7801\u4e0d\u4e00\u81f4,\u4e5f\u53ef\u80fd\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>4.\u7528\u6237\u63d0\u4ea4\u9875\u9762\u7684\u7f16\u7801\u4e0e\u663e\u793a\u6570\u636e\u7684\u9875\u9762\u7f16\u7801\u4e0d\u4e00\u81f4,\u5c31\u80af\u5b9a\u4f1a\u9020\u6210PHP\u9875\u9762\u4e71\u7801.<\/p>\n<p>5.\u5982\u7528\u6237\u8f93\u5165\u8d44\u6599\u7684\u9875\u9762\u662fbig5\u7801, \u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u9875\u9762\u5374\u662fgb2312,\u8fd9\u79cd100%\u4f1a\u9020\u6210PHP\u9875\u9762\u4e71\u7801.<\/p>\n<p>6.PHP\u9875\u9762\u5b57\u7b26\u96c6\u4e0d\u6b63\u786e.<\/p>\n<p>7.PHP\u8fde\u63a5MYSQL\u6570\u636e\u5e93\u8bed\u53e5\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u6b63\u786e.<\/p>\n<p>\u4f7f\u7528mysql+php\u4ea7\u751f\u4e71\u7801\u7684\u539f\u56e0\u90fd\u4e86\u89e3\u5f97\u5f88\u6e05\u695a\u4e86,\u90a3\u4e48\u89e3\u51b3\u5c31\u4e0d\u56f0\u96be\u4e86.<\/p>\n<p>\u9488\u5bf9\u4e0d\u540c\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p>1.mysql\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u7f16\u7801\u662futf8,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684PHP\u7f51\u9875\u4e0d\u4e00\u81f4,\u53ef\u80fd\u5c31\u4f1a\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>\u4fee\u6539\u6570\u636e\u5e93\u7f16\u7801\uff0c\u5982\u679c\u662f\u6570\u636e\u5e93\u7f16\u7801\u4e0d\u6b63\u786e,\u53ef\u4ee5\u5728phpmyadmin \u6267\u884c\u5982\u4e0b\u547d\u4ee4:<\/p>\n<p>ALTER DATABASE &#8216;test&#8217; DEFAULT CHARACTER SET utf8 COLLATE utf8_bin<\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06test\u6570\u636e\u5e93\u7684\u7f16\u7801\u8bbe\u4e3autf8.<\/p>\n<p>2.MYSQL\u4e2d\u521b\u5efa\u8868\u65f6\u4f1a\u8ba9\u4f60\u9009\u62e9\u4e00\u79cd\u7f16\u7801,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684\u7f51\u9875\u7f16\u7801\u4e0d\u4e00\u81f4,\u4e5f\u53ef\u80fd\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>\u4fee\u6539\u8868\u7684\u7f16\u7801\uff1a<\/p>\n<p>ALTER TABLE &#8216;category&#8217; DEFAULT CHARACTER SET utf8 COLLATE utf8_bin<\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06\u4e00\u4e2a\u8868category\u7684\u7f16\u7801\u6539\u4e3autf8.<\/p>\n<p>3.MYSQL\u521b\u5efa\u8868\u65f6\u6dfb\u52a0\u5b57\u6bb5\u662f\u53ef\u4ee5\u9009\u62e9\u7f16\u7801\u7684,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684\u7f51\u9875\u7f16\u7801\u4e0d\u4e00\u81f4,\u4e5f\u53ef\u80fd\u9020\u6210MYSQL\u4e71\u7801.<\/p>\n<p>\u4fee\u6539\u5b57\u6bb5\u7684\u7f16\u7801\uff1a<\/p>\n<p>ALTER TABLE &#8216;test&#8217; CHANGE &#8216;dd&#8217; &#8216;dd&#8217; VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL<\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06test\u8868\u4e2d dd\u7684\u5b57\u6bb5\u7f16\u7801\u6539\u4e3autf8.<\/p>\n<p>4.\u7528\u6237\u63d0\u4ea4\u9875\u9762\u7684\u7f16\u7801\u4e0e\u663e\u793a\u6570\u636e\u7684\u9875\u9762\u7f16\u7801\u4e0d\u4e00\u81f4,\u5c31\u80af\u5b9a\u4f1a\u9020\u6210PHP\u9875\u9762\u4e71\u7801.<\/p>\n<p>\u5982\u679c\u662f\u8fd9\u79cd\u60c5\u51b5\u5bb9\u6613\u89e3\u51b3\uff0c\u53ea\u9700\u68c0\u67e5\u4e0b\u9875\u9762\uff0c\u4fee\u6539\u6e90\u6587\u4ef6\u7684charset\u5373\u53ef.<\/p>\n<p>5.\u5982\u7528\u6237\u8f93\u5165\u8d44\u6599\u7684\u9875\u9762\u662fbig5\u7801, \u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u9875\u9762\u5374\u662fgb2312,\u8fd9\u79cd100%\u4f1a\u9020\u6210PHP\u9875\u9762\u4e71\u7801.<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e5f\u662f\u4fee\u6539\u9875\u9762charset\u5373\u53ef.<\/p>\n<p>6.PHP\u9875\u9762\u5b57\u7b26\u96c6\u4e0d\u6b63\u786e.<\/p>\n<p>\u4e3a\u4e86\u907f\u514dPHP\u9875\u9762\u4e71\u7801\u7684\u53d1\u751f\uff0cPHP\u9875\u9762\u5f00\u59cb\u7b2c\u4e00\u53e5<\/p>\n<p>header(&#8220;content-type:text\/html; charset=utf-8&#8221;);<\/p>\n<p>\/\/\u5f3a\u884c\u6307\u5b9a\u9875\u9762\u7684\u7f16\u7801\uff0c\u4ee5\u907f\u514d\u4e71\u7801<\/p>\n<p>7.PHP\u8fde\u63a5MYSQL\u6570\u636e\u5e93\u8bed\u53e5\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u6b63\u786e.<\/p>\n<p>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u8bed\u53e5\u4e2d.<\/p>\n<p>mysql_connect(&#8216;localhost&#8217;,&#8217;user&#8217;,&#8217;password&#8217;);<\/p>\n<p>mysql_select_db(&#8216;my_db&#8217;);<\/p>\n<p>mysql_query(&#8220;set names utf8;&#8221;); \/\/select \u6570\u636e\u5e93\u4e4b\u540e\u52a0\u591a\u8fd9\u4e00\u53e5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u8bae\u6570\u636e\u5e93\u7528utf8\u7f16\u7801 \u95ee\u9898\u6c47\u603b\uff1a 1.mysql\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u7f16\u7801\u662futf8,\u5982\u679c\u8fd9\u79cd\u7f16\u7801\u4e0e\u4f60\u7684PHP\u7f51\u9875\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":5317,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions\/5317"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}