{"id":188860,"date":"2024-11-12T02:31:22","date_gmt":"2024-11-11T18:31:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188860\/"},"modified":"2024-11-12T02:31:24","modified_gmt":"2024-11-11T18:31:24","slug":"%e6%8e%a2%e6%9e%90php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b3%bb%e7%b5%b1%e7%9a%84%e6%ba%90%e7%a2%bc%e8%a8%ad%e8%a8%88%e8%88%87%e5%af%a6%e7%8f%be-php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b3%bb%e7%b5%b1%e6%ba%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188860\/","title":{"rendered":"\u63a2\u6790PHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08\u8207\u5be6\u73fe (php\u6578\u64da\u5eab\u7cfb\u7d71\u6e90\u78bc)"},"content":{"rendered":"<h1 id=\"%e6%8e%a2%e6%9e%90php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b3%bb%e7%b5%b1%e7%9a%84%e6%ba%90%e7%a2%bc%e8%a8%ad%e8%a8%88%e8%88%87%e5%af%a6%e7%8f%be-OItwTEFPUR\">\u63a2\u6790PHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08\u8207\u5be6\u73fe<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cPHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u958b\u767c\u3002\u5176\u5f37\u5927\u7684\u6578\u64da\u5eab\u652f\u6301\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08\u8207\u5be6\u73fe\uff0c\u5e6b\u52a9\u8b80\u8005\u7406\u89e3\u5176\u80cc\u5f8c\u7684\u539f\u7406\u548c\u6280\u8853\u3002<\/p>\n<h2 id=\"php%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%97%9c%e4%bf%82-OItwTEFPUR\">PHP\u8207\u6578\u64da\u5eab\u7684\u95dc\u4fc2<\/h2>\n<p>PHP\u901a\u5e38\u8207\u591a\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71\u914d\u5408\u4f7f\u7528\uff0c\u5176\u4e2d\u6700\u5e38\u898b\u7684\u662fMySQL\u3002PHP\u63d0\u4f9b\u4e86\u591a\u7a2e\u64f4\u5c55\u4f86\u652f\u6301\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5305\u62ecMySQLi\u548cPDO\uff08PHP Data Objects\uff09\u3002\u9019\u4e9b\u64f4\u5c55\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u57f7\u884cSQL\u67e5\u8a62\u3001\u7372\u53d6\u7d50\u679c\u96c6\u4ee5\u53ca\u8655\u7406\u6578\u64da\u5eab\u932f\u8aa4\u7b49\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%b5%81%e7%a8%8b-OItwTEFPUR\">\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u57fa\u672c\u6d41\u7a0b<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u57fa\u672c\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li><strong>\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/strong>\u4f7f\u7528\u76f8\u61c9\u7684\u64f4\u5c55\u51fd\u6578\u4f86\u9023\u63a5\u6578\u64da\u5eab\u3002<\/li>\n<li><strong>\u57f7\u884cSQL\u67e5\u8a62\uff1a<\/strong>\u4f7f\u7528SQL\u8a9e\u53e5\u4f86\u67e5\u8a62\u6216\u4fee\u6539\u6578\u64da\u3002<\/li>\n<li><strong>\u8655\u7406\u7d50\u679c\u96c6\uff1a<\/strong>\u6839\u64da\u67e5\u8a62\u7d50\u679c\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\u3002<\/li>\n<li><strong>\u95dc\u9589\u9023\u63a5\uff1a<\/strong>\u5728\u64cd\u4f5c\u5b8c\u6210\u5f8c\uff0c\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\u4ee5\u91cb\u653e\u8cc7\u6e90\u3002<\/li>\n<\/ol>\n<h2 id=\"php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b3%bb%e7%b5%b1%e7%9a%84%e6%ba%90%e7%a2%bc%e8%a8%ad%e8%a8%88-OItwTEFPUR\">PHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08<\/h2>\n<p>\u5728\u8a2d\u8a08PHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<h3 id=\"1-%e4%bb%a3%e7%a2%bc%e7%b5%90%e6%a7%8b-OItwTEFPUR\">1. \u4ee3\u78bc\u7d50\u69cb<\/h3>\n<p>\u826f\u597d\u7684\u4ee3\u78bc\u7d50\u69cb\u662f\u78ba\u4fdd\u53ef\u7dad\u8b77\u6027\u548c\u53ef\u64f4\u5c55\u6027\u7684\u95dc\u9375\u3002\u901a\u5e38\uff0c\u958b\u767c\u8005\u6703\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u5c01\u88dd\u5728\u4e00\u500b\u985e\u4e2d\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u65b9\u6cd5\u4f86\u57f7\u884c\u5404\u7a2e\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\nclass Database {\n    private $connection;\n\n    public function __construct($host, $user, $password, $dbname) {\n        $this-&gt;connection = new mysqli($host, $user, $password, $dbname);\n        if ($this-&gt;connection-&gt;connect_error) {\n            die(\"Connection failed: \" . $this-&gt;connection-&gt;connect_error);\n        }\n    }\n\n    public function query($sql) {\n        return $this-&gt;connection-&gt;query($sql);\n    }\n\n    public function close() {\n        $this-&gt;connection-&gt;close();\n    }\n}\n<\/code><\/pre>\n<h3 id=\"2-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-OItwTEFPUR\">2. \u932f\u8aa4\u8655\u7406<\/h3>\n<p>\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\uff0c\u932f\u8aa4\u8655\u7406\u81f3\u95dc\u91cd\u8981\u3002\u958b\u767c\u8005\u61c9\u8a72\u4f7f\u7528\u7570\u5e38\u8655\u7406\u6a5f\u5236\u4f86\u6355\u7372\u548c\u8655\u7406\u932f\u8aa4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\ntry {\n    $db = new Database('localhost', 'user', 'password', 'database');\n    $result = $db-&gt;query(\"SELECT * FROM users\");\n} catch (Exception $e) {\n    echo \"Error: \" . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<h3 id=\"3-%e5%ae%89%e5%85%a8%e6%80%a7-OItwTEFPUR\">3. \u5b89\u5168\u6027<\/h3>\n<p>\u5b89\u5168\u6027\u662f\u8a2d\u8a08\u6578\u64da\u5eab\u7cfb\u7d71\u6642\u5fc5\u9808\u8003\u616e\u7684\u91cd\u8981\u56e0\u7d20\u3002\u958b\u767c\u8005\u61c9\u8a72\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\n$stmt = $db-&gt;connection-&gt;prepare(\"SELECT * FROM users WHERE email = ?\");\n$stmt-&gt;bind_param(\"s\", $email);\n$stmt-&gt;execute();\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OItwTEFPUR\">\u7e3d\u7d50<\/h2>\n<p>PHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08\u8207\u5be6\u73fe\u6d89\u53ca\u591a\u500b\u65b9\u9762\uff0c\u5305\u62ec\u4ee3\u78bc\u7d50\u69cb\u3001\u932f\u8aa4\u8655\u7406\u548c\u5b89\u5168\u6027\u7b49\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u69cb\u5efa\u51fa\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u7cfb\u7d71\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0ePHP\u6578\u64da\u5eab\u7cfb\u7d71\u7684\u6e90\u78bc\u8a2d\u8a08\u8207\u5be6\u73fe\uff0c\u63ed\u793a\u5176\u67b6\u69cb\u3001\u529f\u80fd\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u529b\u958b\u767c\u8005\u63d0\u5347\u6280\u8853\u6c34\u5e73\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-188860","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188860","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=188860"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188860\/revisions"}],"predecessor-version":[{"id":188861,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188860\/revisions\/188861"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}