{"id":522,"date":"2010-09-26T09:11:49","date_gmt":"2010-09-26T09:11:49","guid":{"rendered":""},"modified":"2010-09-26T09:11:49","modified_gmt":"2010-09-26T09:11:49","slug":"522","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=522","title":{"rendered":"MySQL+PHP\u4e71\u7801\u539f\u7406\u4e0e\u89e3\u51b3"},"content":{"rendered":"<p>MySQL+PHP\u4ea7\u751f\u4e71\u7801\u539f\u56e0\uff1a <\/p>\n<p>\u25c6 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\uff1b <\/p>\n<p>\u25c6 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\uff1b <\/p>\n<p>\u25c6 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\uff1b <\/p>\n<p>\u25c6 \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\uff1b <\/p>\n<p>\u25c6 \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\uff1b <\/p>\n<p>\u25c6 PHP\u9875\u9762\u5b57\u7b26\u96c6\u4e0d\u6b63\u786e\uff1b <\/p>\n<p>\u25c6 PHP\u8fde\u63a5MySQL\u6570\u636e\u5e93\u8bed\u53e5\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u6b63\u786e\u3002 <\/p>\n<p>\u6ce8\u610f\uff1a <\/p>\n<p>\u5f88\u591a\u4eba\u90fd\u6000\u7591MySQL\u7248\u672c\u4e0d\u4e00\u81f4\u4f1a\u5bfc\u81f4\u4e71\u7801,\u76f8\u4fe1\u770b\u4e86\u672c\u8bf4\u660e\u4f60\u5c31\u4e0d\u4f1a\u8fd9\u6837\u8ba4\u4e3a\u4e86\u3002 <\/p>\n<p>\u5e73\u65f6\u4f60\u5728\u67d0\u4e9b\u7f51\u7ad9\u770b\u5230\u7684\u6587\u5b57\u53ef\u80fd\u6709\u51e0\u79cd\u7f16\u7801, \u5982\u4f60\u770b\u5230\u4e00\u4e2a\u7e41\u4f53\u5b57,\u5b83\u6709\u53ef\u80fd\u662fbig5\u7f16\u7801,\u4e5f\u6709 \u53ef\u80fd\u662futf-8\u7f16\u7801\u7684,\u66f4\u6709\u53ef\u80fd\u662fgb\u7801\u7684,\u6ca1\u9519,\u4e5f\u5c31\u662f\u8bf4\u6709\u7b80\u4f53\u7f16\u7801\u7684\u7e41\u4f53\u5b57,\u4e5f\u6709\u7e41\u4f53\u7f16\u7801\u7684\u7b80\u4f53\u5b57,\u4e00\u5b9a\u8981\u4e86\u89e3\u8fd9\u4e00\u70b9\u3002 <\/p>\n<p>\u5982\u679c\u4f60\u662f\u505a\u4e00\u4e2a\u7b80\u4f53\u7f16\u7801\u7684\u7f51\u9875,\u7f16\u7801\u5b9a\u4e3aGB2312,\u5982\u679c\u6709\u9999\u6e2f\u548c\u53f0\u6e7e\u5730\u533a\u7684\u8bbf\u5ba2\u63d0\u4ea4\u7e41\u4f53\u7684\u4fe1\u606f,\u5c31\u53ef\u80fd\u4f1a\u9020\u6210\u4e71\u7801,\u89e3\u51b3\u65b9\u6cd5\uff1a <\/p>\n<p>\u5c06\u7f51\u7ad9\u7f16\u7801\u8bbe\u4e3a utf-8,\u8fd9\u6837\u53ef\u4ee5\u517c\u5bb9\u4e16\u754c\u4e0a\u6240\u6709\u5b57\u7b26\u3002 <\/p>\n<p>\u5982\u679c\u7f51\u7ad9\u5df2\u7ecf\u8fd0\u4f5c\u4e86\u597d\u4e45,\u5df2\u6709\u5f88\u591a\u65e7\u6570\u636e,\u4e0d\u80fd\u518d\u66f4\u6539\u7b80\u4f53\u4e2d\u6587\u7684\u8bbe\u5b9a,\u90a3\u4e48\u5efa\u8bae\u5c06\u9875\u9762\u7684\u7f16\u7801\u8bbe\u4e3a GBK, GBK\u4e0eGB2312\u7684\u533a\u522b\u5c31\u5728\u4e8e:GBK\u80fd\u6bd4GB2312\u663e\u793a\u66f4\u591a\u7684\u5b57\u7b26,\u8981\u663e\u793a\u7b80\u4f53\u7801\u7684\u7e41\u4f53\u5b57,\u5c31\u53ea\u80fd\u7528GBK\u3002 <\/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\u3002 <\/p>\n<p>MySQL+PHP\u4ea7\u751f\u4e71\u7801\u7684\u89e3\u51b3\u529e\u6cd5\uff1a <\/p>\n<p>\u5982\u679c\u5b89\u88c5MySQL\u7684\u7f16\u7801\u5df2\u4e0d\u80fd\u66f4\u6539,\u5f88\u591a\u670b\u53cb\u662f\u8d2d\u4e70\u865a\u62df\u4e3b\u673a\u5efa\u7acb\u7f51\u7ad9,\u65e0\u6743\u66f4\u6539MySQL\u7684\u5b89\u88c5\u7f16\u7801,\u8fd9\u4e00\u5173\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7,\u56e0\u4e3a\u53ea\u8981\u540e\u9762\u7684\u6b65\u805a\u6b63\u786e,\u4e00\u6837\u80fd\u89e3\u51b3\u4e71\u7801\u95ee\u9898\u3002 <\/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\uff1a <br \/><code>Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin<\/code><\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06test\u6570\u636e\u5e93\u7684\u7f16\u7801\u8bbe\u4e3autf8\u3002 <\/p>\n<p>\u4fee\u6539\u8868\u7684\u7f16\u7801\uff1a <br \/><code>Alter TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin<\/code><\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06\u4e00\u4e2a\u8868category\u7684\u7f16\u7801\u6539\u4e3autf8\u3002 <\/p>\n<p>\u4fee\u6539\u5b57\u6bb5\u7684\u7f16\u7801\uff1a <br \/><code>Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER <br \/>SET utf8 COLLATE utf8_bin NOT NULL<\/code><\/p>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c31\u662f\u5c06test\u8868\u4e2ddd\u7684\u5b57\u6bb5\u7f16\u7801\u6539\u4e3autf8\u3002 <\/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\u3002 <\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e5f\u662f\u4fee\u6539\u9875\u9762charset\u5373\u53ef\u3002 <\/p>\n<p>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u8bed\u53e5\u4e2d\u3002 <br \/><code><br \/>mysql_connect('localhost','user','password'); <br \/>mysql_select_db('my_db'); <br \/>mysql_query(\"set names utf8;\");&nbsp;&nbsp;&nbsp;&nbsp; \/\/select \u6570\u636e\u5e93\u4e4b\u540e\u52a0\u591a\u8fd9\u4e00\u53e5<br \/><\/code><\/p>\n<p>\u4e3a\u4e86\u907f\u514dPHP\u9875\u9762\u4e71\u7801\u7684\u53d1\u751f\uff0cPHP\u9875\u9762\u5f00\u59cb\u7b2c\u4e00\u53e5 <br \/><code>header(\"content-type:text\/html; charset=utf-8\");<\/code><\/p>\n<p>\/\/\u5f3a\u884c\u6307\u5b9a\u9875\u9762\u7684\u7f16\u7801\uff0c\u4ee5\u907f\u514d\u4e71\u7801 <br \/>\u6ce8\u610f\uff1a\u7167\u4ee5\u4e0a\u65b9\u6cd5\u4fee\u6539\u4ee5\u540e\u53ea\u80fd\u4fdd\u8bc1\u4f60\u65b0\u63d2\u5165\u7684\u6570\u636e\u4e0d\u4f1a\u4e71\u7801,\u4e3e\u4e2a\u4f8b:\u5982\u679c\u4f60\u7528\u6237\u5df2\u63d0\u4ea4\u7684\u6570\u636e\u662fBIG5,\u4f60\u5374\u60f3\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u6539\u4e3a\u53ef\u4ee5\u5728GB2312\u7684\u7f51\u9875\u6b63\u786e\u663e\u793a\u662f\u4e0d\u53ef\u80fd\u7684, \u8fd9\u79cd\u6587\u5b57\u5185\u7801\u7684\u53d8\u6362\u53ea\u80fd\u901a\u8fc7\u53e6\u5199\u7a0b\u5e8f\u6765\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL+PHP\u4ea7\u751f\u4e71\u7801\u539f\u56e0\uff1a \u25c6 MySQL\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u7f16\u7801\u662fu&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-522","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=522"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}