{"id":268,"date":"2006-05-16T22:03:58","date_gmt":"2006-05-16T14:03:58","guid":{"rendered":"http:\/\/j178.mtgbb.com\/68"},"modified":"2006-05-16T22:03:58","modified_gmt":"2006-05-16T14:03:58","slug":"mysql%e5%92%8cphp%e6%8e%a1%e7%94%a8utf8%e7%9a%84%e8%a9%b3%e7%b4%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/j178.mtgbb.com\/?p=268","title":{"rendered":"MySQL\u548cphp\u63a1\u7528UTF8\u7684\u8a73\u7d30\u65b9\u6cd5"},"content":{"rendered":"<p>\u8cc7\u6599\u4f86\u6e90 : <a href=\"http:\/\/phorum.study-area.org\/viewtopic.php?t=36484&amp;highlight=utf8\">\u9177\uff01\u5b78\u5712\u8a0e\u8ad6\u5340<\/a><\/p>\n<p>****** \u7db2 \u7ad9 \u5168 \u9762 \u63a1 \u7528 UTF-8 \u65b9\u6cd5. ******<br \/>\n1. \u7528 vi \/etc\/httpd\/conf\/httpd.conf \u8a2d\u5b9aApache\u4e2d\u7684\u8a9e\u4fc2\u70ba (\u8a18\u5f97restart)<br \/>\nAddDefaultCharset UTF-8<\/p>\n<p>2. \u7528 vi \/etc\/php.ini \u8a2d\u5b9aphp\u4e2d\u7684\u8a9e\u4fc2\u70ba (\u8a18\u5f97restart)<br \/>\ndefault_charset = &#8220;utf-8&#8221;<\/p>\n<p><!--more--><\/p>\n<p>3. \u7528 vi \/etc\/my.cnf \u8a2d\u5b9aMySQL\u4e2d\u7684\u8a9e\u4fc2\u70ba (\u8a18\u5f97restart)<\/p>\n<p>[mysqld]<br \/>\ninit_connect=&#8217;SET NAMES utf8&#8242;<br \/>\ndefault-character-set=utf8<\/p>\n<p>[client]<br \/>\ndefault-character-set = utf8<\/p>\n<p>4. \u5efa\u7acb\u8cc7\u6599\u5eab\u6642\u9078\u64c7\u8a9e\u7cfb: (\u8a18\u5f97\u6e05\u9664DB Cache)<\/p>\n<p>DROP DATABASE IF EXISTS `aa`;<\/p>\n<p>CREATE DATABASE `aa` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;<\/p>\n<p>USE `aa`;CREATE TABLE IF NOT EXISTS `aat` (<br \/>\n`id` char(1) NOT NULL default &#8216;1&#8217;,<br \/>\n`myStr` varchar(200) default NULL,<br \/>\nPRIMARY KEY (`id`)<br \/>\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;<\/p>\n<p>5. \u7528UltraEdit(v11.20a\u7248) \u8f49\u63db\u6240\u6709ANSI\u683c\u5f0f\u7684php\u6a94\u6848\u8f49\u5316\u70baUTF-8\u683c\u5f0f:<\/p>\n<p>File &#8211;&gt; Conversions &#8211;&gt; ASCII to UTF-8 (Unicoding Editing)<\/p>\n<p>( \u5728UltraEdit\u4e2d\u6309Advanced &#8211;&gt; configuration &#8211;&gt; File Handling<\/p>\n<p>&#8211;&gt; Unicode\/UTF-8 Detection &#8211;&gt; \u5254\u9078Auto detect utf-8 files )<\/p>\n<p>\u5982\u6709\u9700\u8981\u6642,\u53ef\u57f7\u884cRemove BOM.php. \u7576\u7528WinXP\u7684Notepad\u5c07php\u6a94\u7531ANSI\u8f49\u70baUTF-8\u6642,<\/p>\n<p>\u56e0\u6a94\u982d\u6709BOM,\u6703\u5f15\u8d77\u6392\u7248\u554f\u984c,\u6545\u5fc5\u9808\u79fb\u9664,\u57f7\u884cRemove BOM.php\u5373\u53ef\u81ea\u52d5\u79fb\u9664.<\/p>\n<p>Remove BOM.php\u53ef\u7531\u4ee5\u4e0b\u7db2\u5740\u4e0b\u8f09:<\/p>\n<p><a href=\"http:\/\/www.hoyo.idv.tw\/hoyoweb\/document\/view.php?sid=13&amp;author=hoyo&amp;status=view\">http:\/\/www.hoyo.idv.tw\/hoyoweb\/document\/view.php?sid=13&amp;author=hoyo&amp;status=view<\/a><\/p>\n<p>6. \u5728php\u6a94\u4e2d\u5fc5\u9808\u52a0\u5165:<\/p>\n<p>&lt;html&gt;&lt;head&gt;<\/p>\n<p>&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt;<\/p>\n<p>&lt;\/head&gt;&lt;body&gt;<\/p>\n<p>7. \u5728\u9023\u63a5DB\u7684\u6a94\u4e2d\u5fc5\u9808\u52a0\u51653\u884cmysql_query\u624dok:<\/p>\n<p>$host=&#8221;localhost&#8221;; $DBname=&#8221;aa&#8221;;<\/p>\n<p>$user= &#8220;root&#8221;; $passwd = &#8220;&#8221;;<\/p>\n<p>$link = mysql_connect($host,$user,$passwd) or die (&#8220;Fail&#8221;);<\/p>\n<p>$db = mysql_select_db($DBname, $link) or die (&#8220;Fail&#8221;);<\/p>\n<p>\/\/ \u8981\u5728\u771f\u6b63query DB\u53d6\u51fa\u8cc7\u6599\u524d,\u52a0\u5165\u4ee5\u4e0b3\u884c<\/p>\n<p>mysql_query(&#8220;SET NAMES &#8216;utf8&#8242;&#8221;);<\/p>\n<p>mysql_query(&#8220;SET CHARACTER_SET_CLIENT=utf8&#8221;);<\/p>\n<p>mysql_query(&#8220;SET CHARACTER_SET_RESULTS=utf8&#8221;);<\/p>\n<p>$sql = &#8220;select * from aat where crid=&#8217;1&#8242;&#8221;;<\/p>\n<p>$rows = mysql_query($sql);<\/p>\n<p>8. \u5728php\u6a94\u4e2d, \u5982\u6709\u9700\u8981\u9808\u6ce8\u610f: [Optional]<\/p>\n<p>\u904b\u7528htmlentities\u548chtmlspecialchars\u6642,\u8981\u4f3c\u5982\u4e0b:<\/p>\n<p>$chars = htmlentities($chars,ENT_QUOTES,&#8221;UTF-8&#8243;);<\/p>\n<p>$chars = htmlspecialchars($chars,ENT_QUOTES,&#8221;UTF-8&#8243;);<\/p>\n<p>\u4e26\u4e14\u5728\u986f\u793a\u524d\u8981\u7528<\/p>\n<p>$chars = html_entity_decode($chars,ENT_QUOTES,&#8221;UTF8&#8243;);<\/p>\n<p>\u5982\u6709\u7528\u904eaddslashes()\u6216mysql_real_escape_string()\u8a18\u5f97\u7528\u4ee5\u4e0b:<\/p>\n<p>$chars = stripslashes($chars);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8cc7\u6599\u4f86\u6e90 : \u9177\uff01\u5b78\u5712\u8a0e\u8ad6\u5340 ****** \u7db2 \u7ad9 \u5168 \u9762 \u63a1 \u7528 UTF-8 \u65b9\u6cd5. ****** 1. \u7528 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[30],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-php","tag-mysql"],"_links":{"self":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=268"}],"version-history":[{"count":0,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/268\/revisions"}],"wp:attachment":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}