{"id":163325,"date":"2024-11-04T22:34:21","date_gmt":"2024-11-04T14:34:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/163325\/"},"modified":"2024-11-04T22:34:22","modified_gmt":"2024-11-04T14:34:22","slug":"%e8%a7%a3%e6%b1%baphp%e8%bc%b8%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%ef%bc%88php%e8%bc%b8%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/163325\/","title":{"rendered":"\u89e3\u6c7aPHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff08PHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e8%a7%a3%e6%b1%baphp%e8%bc%b8%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c-VokwlWdNbc\">\u89e3\u6c7aPHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cPHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7db2\u7ad9\u958b\u767c\u4e2d\u3002\u96a8\u8457\u4e2d\u6587\u5167\u5bb9\u7684\u666e\u53ca\uff0c\u8a31\u591a\u958b\u767c\u8005\u5728\u4f7f\u7528PHP\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u6642\uff0c\u7d93\u5e38\u6703\u9047\u5230\u4e2d\u6587\u4e82\u78bc\u7684\u554f\u984c\u3002\u9019\u4e0d\u50c5\u5f71\u97ff\u4e86\u7528\u6236\u9ad4\u9a57\uff0c\u4e5f\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u7684\u932f\u8aa4\u8655\u7406\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u9020\u6210PHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u7684\u539f\u56e0\uff0c\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%9a%84%e5%8e%9f%e5%9b%a0-VokwlWdNbc\">\u4e2d\u6587\u4e82\u78bc\u7684\u539f\u56e0<\/h2>\n<p>\u4e2d\u6587\u4e82\u78bc\u901a\u5e38\u662f\u7531\u65bc\u5b57\u7b26\u7de8\u78bc\u4e0d\u4e00\u81f4\u6240\u5f15\u8d77\u7684\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u5e38\u898b\u7684\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5eab\u5b57\u7b26\u96c6\u8a2d\u7f6e\u4e0d\u6b63\u78ba\uff1a<\/strong>\u5982\u679c\u6578\u64da\u5eab\u7684\u5b57\u7b26\u96c6\u8a2d\u7f6e\u70ba\u4e0d\u652f\u6301\u4e2d\u6587\u7684\u7de8\u78bc\uff08\u5982latin1\uff09\uff0c\u90a3\u9ebc\u5b58\u5132\u7684\u4e2d\u6587\u5b57\u7b26\u5c07\u7121\u6cd5\u6b63\u78ba\u986f\u793a\u3002<\/li>\n<li><strong>PHP\u6587\u4ef6\u7de8\u78bc\u554f\u984c\uff1a<\/strong>\u5982\u679cPHP\u6587\u4ef6\u672c\u8eab\u7684\u7de8\u78bc\u8207\u6578\u64da\u5eab\u7684\u7de8\u78bc\u4e0d\u4e00\u81f4\uff0c\u5247\u5728\u8f38\u51fa\u6642\u53ef\u80fd\u6703\u51fa\u73fe\u4e82\u78bc\u3002<\/li>\n<li><strong>HTTP\u97ff\u61c9\u982d\u672a\u8a2d\u7f6e\u6b63\u78ba\uff1a<\/strong>\u5982\u679cHTTP\u97ff\u61c9\u982d\u672a\u6b63\u78ba\u8a2d\u7f6e\u70baUTF-8\uff0c\u5247\u700f\u89bd\u5668\u53ef\u80fd\u7121\u6cd5\u6b63\u78ba\u89e3\u8b80\u8f38\u51fa\u7684\u5167\u5bb9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-VokwlWdNbc\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u70ba\u4e86\u89e3\u6c7aPHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\u8a2d\u7f6e\uff1a<\/p>\n<h3 id=\"1-%e7%a2%ba%e4%bf%9d%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%97%e7%ac%a6%e9%9b%86%e8%a8%ad%e7%bd%ae%e7%82%bautf-8-VokwlWdNbc\">1. \u78ba\u4fdd\u6578\u64da\u5eab\u5b57\u7b26\u96c6\u8a2d\u7f6e\u70baUTF-8<\/h3>\n<p>\u5728\u5275\u5efa\u6578\u64da\u5eab\u6642\uff0c\u61c9\u78ba\u4fdd\u4f7f\u7528UTF-8\u5b57\u7b26\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u8a9e\u53e5\u4f86\u6aa2\u67e5\u548c\u8a2d\u7f6e\u6578\u64da\u5eab\u7684\u5b57\u7b26\u96c6\uff1a<\/p>\n<pre><code>SHOW VARIABLES LIKE 'character_set_database';\nALTER DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u5c0d\u65bc\u6bcf\u500b\u8868\u548c\u5217\uff0c\u4e5f\u61c9\u78ba\u4fdd\u5b83\u5011\u7684\u5b57\u7b26\u96c6\u8a2d\u7f6e\u70baUTF-8\uff1a<\/p>\n<pre><code>ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<h3 id=\"2-%e8%a8%ad%e7%bd%aephp%e6%96%87%e4%bb%b6%e7%b7%a8%e7%a2%bc-VokwlWdNbc\">2. \u8a2d\u7f6ePHP\u6587\u4ef6\u7de8\u78bc<\/h3>\n<p>\u78ba\u4fddPHP\u6587\u4ef6\u7684\u7de8\u78bc\u70baUTF-8\u7121BOM\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\uff08\u5982VS Code\u6216Notepad++\uff09\u4f86\u6aa2\u67e5\u548c\u66f4\u6539\u6587\u4ef6\u7de8\u78bc\u3002<\/p>\n<h3 id=\"3-%e8%a8%ad%e7%bd%aehttp%e9%9f%bf%e6%87%89%e9%a0%ad-VokwlWdNbc\">3. \u8a2d\u7f6eHTTP\u97ff\u61c9\u982d<\/h3>\n<p>\u5728PHP\u8173\u672c\u4e2d\uff0c\u61c9\u8a72\u5728\u8f38\u51fa\u4efb\u4f55\u5167\u5bb9\u4e4b\u524d\u8a2d\u7f6e\u6b63\u78ba\u7684HTTP\u97ff\u61c9\u982d\uff0c\u4ee5\u544a\u8a34\u700f\u89bd\u5668\u4f7f\u7528UTF-8\u7de8\u78bc\uff1a<\/p>\n<pre><code>header('Content-Type: text\/html; charset=utf-8');<\/code><\/pre>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8mysqli%e6%88%96pdo%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-VokwlWdNbc\">4. \u4f7f\u7528mysqli\u6216PDO\u9023\u63a5\u6578\u64da\u5eab<\/h3>\n<p>\u5728\u4f7f\u7528mysqli\u6216PDO\u9023\u63a5\u6578\u64da\u5eab\u6642\uff0c\u61c9\u660e\u78ba\u8a2d\u7f6e\u5b57\u7b26\u96c6\u70baUTF-8\uff1a<\/p>\n<pre><code>$mysqli = new mysqli(\"localhost\", \"username\", \"password\", \"database\");\n$mysqli-&gt;set_charset(\"utf8mb4\");<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528PDO\uff1a<\/p>\n<pre><code>$pdo = new PDO(\"mysql:host=localhost;dbname=database;charset=utf8mb4\", \"username\", \"password\");<\/code><\/pre>\n<h2 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-VokwlWdNbc\">\u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6b63\u78ba\u8a2d\u7f6ePHP\u8207MySQL\u7684\u9023\u63a5\uff0c\u4ee5\u907f\u514d\u4e2d\u6587\u4e82\u78bc\uff1a<\/p>\n<pre><code>&lt;?php\nheader('Content-Type: text\/html; charset=utf-8');\n$mysqli = new mysqli(\"localhost\", \"username\", \"password\", \"database\");\n$mysqli-&gt;set_charset(\"utf8mb4\");\n\nif ($mysqli-&gt;connect_error) {\n    die(\"Connection failed: \" . $mysqli-&gt;connect_error);\n}\n\n$result = $mysqli-&gt;query(\"SELECT * FROM your_table_name\");\nwhile ($row = $result-&gt;fetch_assoc()) {\n    echo $row['your_column_name'] . \"&lt;br&gt;\";\n}\n$mysqli-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VokwlWdNbc\">\u7e3d\u7d50<\/h2>\n<p>\u89e3\u6c7aPHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\u7684\u95dc\u9375\u5728\u65bc\u78ba\u4fdd\u5b57\u7b26\u96c6\u7684\u4e00\u81f4\u6027\uff0c\u5305\u62ec\u6578\u64da\u5eab\u3001PHP\u6587\u4ef6\u548cHTTP\u97ff\u61c9\u982d\u7684\u8a2d\u7f6e\u3002\u901a\u904e\u9075\u5faa\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u4e2d\u6587\u4e82\u78bc\u7684\u554f\u984c\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684PHP\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u7684\u4f3a\u670d\u5668\u652f\u6301\uff0c\u52a9\u60a8\u9806\u5229\u958b\u767c\u548c\u904b\u884c\u61c9\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7aPHP\u8f38\u51fa\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u63d0\u4f9b\u6709\u6548\u65b9\u6cd5\u8207\u6280\u5de7\uff0c\u78ba\u4fdd\u4e2d\u6587\u6578\u64da\u6b63\u78ba\u986f\u793a\uff0c\u63d0\u5347\u7db2\u7ad9\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-163325","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=163325"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163325\/revisions"}],"predecessor-version":[{"id":163326,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163325\/revisions\/163326"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=163325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=163325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=163325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}