{"id":188592,"date":"2024-11-12T00:04:32","date_gmt":"2024-11-11T16:04:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188592\/"},"modified":"2024-11-12T00:04:33","modified_gmt":"2024-11-11T16:04:33","slug":"%e6%8e%a2%e7%b4%a2php%e6%95%b8%e6%93%9a%e5%ba%ab%e6%96%b9%e6%b3%95%ef%bc%8c%e5%85%a8%e9%9d%a2%e4%ba%86%e8%a7%a3%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e5%92%8c%e7%ae%a1%e7%90%86%e6%8a%80%e5%b7%a7-php","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188592\/","title":{"rendered":"\u63a2\u7d22PHP\u6578\u64da\u5eab\u65b9\u6cd5\uff0c\u5168\u9762\u4e86\u89e3\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u6280\u5de7 (php\u6578\u64da\u5eab\u6709\u54ea\u4e9b\u65b9\u6cd5)"},"content":{"rendered":"<h1 id=\"%e6%8e%a2%e7%b4%a2php%e6%95%b8%e6%93%9a%e5%ba%ab%e6%96%b9%e6%b3%95%ef%bc%8c%e5%85%a8%e9%9d%a2%e4%ba%86%e8%a7%a3%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e5%92%8c%e7%ae%a1%e7%90%86%e6%8a%80%e5%b7%a7-srhNIAGSPN\">\u63a2\u7d22PHP\u6578\u64da\u5eab\u65b9\u6cd5\uff0c\u5168\u9762\u4e86\u89e3\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u6280\u5de7<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePHP\u4e2d\u5e38\u7528\u7684\u6578\u64da\u5eab\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5168\u9762\u4e86\u89e3\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u7684\u6280\u5de7\u3002<\/p>\n<h2 id=\"1-php%e8%88%87mysql%e7%9a%84%e7%b5%90%e5%90%88-srhNIAGSPN\">1. PHP\u8207MySQL\u7684\u7d50\u5408<\/h2>\n<p>MySQL\u662f\u6700\u5e38\u7528\u7684\u958b\u6e90\u6578\u64da\u5eab\u4e4b\u4e00\uff0c\u8207PHP\u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code>\nconnect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\necho \"\u9023\u63a5\u6210\u529f\";\n?&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86mysqli\u64f4\u5c55\u4f86\u5efa\u7acb\u8207MySQL\u7684\u9023\u63a5\u3002\u9019\u662fPHP\u4e2d\u6700\u5e38\u898b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<h2 id=\"2-pdo%ef%bc%88php-data-objects%ef%bc%89-srhNIAGSPN\">2. PDO\uff08PHP Data Objects\uff09<\/h2>\n<p>PDO\u662f\u4e00\u7a2e\u66f4\u9748\u6d3b\u7684\u6578\u64da\u5eab\u8a2a\u554f\u5c64\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u4f7f\u7528PDO\u7684\u597d\u8655\u5728\u65bc\u5b83\u63d0\u4f9b\u4e86\u9810\u8655\u7406\u8a9e\u53e5\uff0c\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u9023\u63a5MySQL\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nsetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    echo \"\u9023\u63a5\u6210\u529f\";\n} catch(PDOException $e) {\n    echo \"\u9023\u63a5\u5931\u6557: \" . $e-&gt;getMessage();\n}\n?&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528try-catch\u584a\u4f86\u6355\u7372\u53ef\u80fd\u7684\u7570\u5e38\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u597d\u5730\u8655\u7406\u932f\u8aa4\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8orm%ef%bc%88%e5%b0%8d%e8%b1%a1%e9%97%9c%e4%bf%82%e6%98%a0%e5%b0%84%ef%bc%89-srhNIAGSPN\">3. \u4f7f\u7528ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09<\/h2>\n<p>ORM\u662f\u4e00\u7a2e\u5c07\u6578\u64da\u5eab\u8868\u6620\u5c04\u5230\u5c0d\u8c61\u7684\u6280\u8853\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u5c0d\u8c61\u5c0e\u5411\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5c\u6578\u64da\u3002Laravel\u6846\u67b6\u4e2d\u7684Eloquent ORM\u662f\u6700\u53d7\u6b61\u8fce\u7684\u9078\u64c7\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Eloquent\u9032\u884c\u6578\u64da\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\naddConnection([\n    'driver' =&gt; 'mysql',\n    'host' =&gt; 'localhost',\n    'database' =&gt; 'database_name',\n    'username' =&gt; 'username',\n    'password' =&gt; 'password',\n    'charset' =&gt; 'utf8',\n    'collation' =&gt; 'utf8_unicode_ci',\n    'prefix' =&gt; '',\n]);\n\n$capsule-&gt;setAsGlobal();\n$capsule-&gt;bootEloquent();\n\n\/\/ \u5275\u5efa\u65b0\u8a18\u9304\n$capsule::table('users')-&gt;insert([\n    'name' =&gt; 'John Doe',\n    'email' =&gt; 'john@example.com',\n]);\n?&gt;\n<\/code><\/pre>\n<p>\u4f7f\u7528ORM\u53ef\u4ee5\u5927\u5927\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2 id=\"4-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-srhNIAGSPN\">4. \u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\uff1a<\/strong>\u9019\u53ef\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002<\/li>\n<li><strong>\u5b9a\u671f\u5099\u4efd\u6578\u64da\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u610f\u5916\u60c5\u6cc1\u800c\u4e1f\u5931\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e8b\u52d9\u8655\u7406\uff1a<\/strong>\u5728\u9032\u884c\u591a\u500b\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u4f7f\u7528\u4e8b\u52d9\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u512a\u5316\u67e5\u8a62\uff1a<\/strong>\u4f7f\u7528\u7d22\u5f15\u548c\u9069\u7576\u7684\u67e5\u8a62\u7d50\u69cb\u4f86\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-srhNIAGSPN\">\u7e3d\u7d50<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u6709\u591a\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u5305\u62ec\u4f7f\u7528MySQLi\u3001PDO\u548cORM\u7b49\u6280\u8853\u3002\u6bcf\u7a2e\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u9ede\u548c\u9069\u7528\u5834\u666f\uff0c\u958b\u767c\u8005\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\u4f86\u9032\u884c\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u3002\u7121\u8ad6\u662f\u9078\u64c7\u50b3\u7d71\u7684MySQL\u9084\u662f\u73fe\u4ee3\u7684ORM\uff0c\u638c\u63e1\u9019\u4e9b\u6280\u8853\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u958b\u767c\u6548\u7387\u548c\u6578\u64da\u5b89\u5168\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684PHP\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22PHP\u6578\u64da\u5eab\u65b9\u6cd5\uff0c\u6df1\u5165\u4e86\u89e3\u6578\u64da\u5b58\u5132\u8207\u7ba1\u7406\u6280\u5de7\uff0c\u63d0\u5347\u60a8\u7684\u958b\u767c\u6548\u7387\u8207\u6578\u64da\u8655\u7406\u80fd\u529b\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-188592","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188592","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=188592"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188592\/revisions"}],"predecessor-version":[{"id":188593,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188592\/revisions\/188593"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}