{"id":96556,"date":"2024-10-20T10:26:18","date_gmt":"2024-10-20T02:26:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96556\/"},"modified":"2024-10-20T10:26:19","modified_gmt":"2024-10-20T02:26:19","slug":"php%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%87%e6%8f%9b-php-%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%87%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96556\/","title":{"rendered":"php\u8f15\u9b06\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db (php \u591a\u6578\u64da\u5eab\u5207\u63db)"},"content":{"rendered":"<h1 id=\"php%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%87%e6%8f%9b-KmrTIPwgbK\">PHP\u8f15\u9b06\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u5df2\u7d93\u6210\u70ba\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u96a8\u8457\u61c9\u7528\u9700\u6c42\u7684\u591a\u6a23\u5316\uff0c\u958b\u767c\u8005\u5e38\u5e38\u9700\u8981\u5728\u4e0d\u540c\u7684\u6578\u64da\u5eab\u4e4b\u9593\u9032\u884c\u5207\u63db\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u63a2\u8a0e\u5982\u4f55\u5728PHP\u4e2d\u8f15\u9b06\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%87%e6%8f%9b%ef%bc%9f-KmrTIPwgbK\">\u70ba\u4ec0\u9ebc\u9700\u8981\u591a\u6578\u64da\u5eab\u5207\u63db\uff1f<\/h2>\n<p>\u591a\u6578\u64da\u5eab\u5207\u63db\u7684\u9700\u6c42\u4e3b\u8981\u4f86\u81ea\u65bc\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u9694\u96e2\uff1a<\/strong>\u4e0d\u540c\u7684\u61c9\u7528\u6216\u6a21\u584a\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u6578\u64da\u5eab\uff0c\u4ee5\u4fbf\u65bc\u6578\u64da\u7684\u7ba1\u7406\u548c\u7dad\u8b77\u3002<\/li>\n<li><strong>\u6027\u80fd\u512a\u5316\uff1a<\/strong>\u6839\u64da\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\uff0c\u9078\u64c7\u6700\u9069\u5408\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u4f86\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u6578\u64da\u5b89\u5168\uff1a<\/strong>\u5c07\u654f\u611f\u6578\u64da\u8207\u5176\u4ed6\u6578\u64da\u5206\u958b\u5b58\u5132\uff0c\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"php%e4%b8%ad%e5%af%a6%e7%8f%be%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%87%e6%8f%9b%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95-KmrTIPwgbK\">PHP\u4e2d\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\u7684\u57fa\u672c\u65b9\u6cd5<\/h2>\n<p>\u5728PHP\u4e2d\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\u4f86\u5b8c\u6210\uff1a<\/p>\n<h3 id=\"1-%e5%ae%9a%e7%be%a9%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-KmrTIPwgbK\">1. \u5b9a\u7fa9\u6578\u64da\u5eab\u914d\u7f6e<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e0d\u540c\u6578\u64da\u5eab\u7684\u914d\u7f6e\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4e00\u500b\u6578\u7d44\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>\n$dbConfig = [\n    'db1' =&gt; [\n        'host' =&gt; 'localhost',\n        'username' =&gt; 'user1',\n        'password' =&gt; 'password1',\n        'database' =&gt; 'database1'\n    ],\n    'db2' =&gt; [\n        'host' =&gt; 'localhost',\n        'username' =&gt; 'user2',\n        'password' =&gt; 'password2',\n        'database' =&gt; 'database2'\n    ]\n];\n<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e5%87%bd%e6%95%b8-KmrTIPwgbK\">2. \u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5\u51fd\u6578<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u51fd\u6578\u4f86\u6839\u64da\u50b3\u5165\u7684\u6578\u64da\u5eab\u540d\u7a31\u4f86\u5efa\u7acb\u9023\u63a5\uff1a<\/p>\n<pre><code>\nfunction getConnection($dbName) {\n    global $dbConfig;\n    $config = $dbConfig[$dbName];\n    \n    $connection = new mysqli($config['host'], $config['username'], $config['password'], $config['database']);\n    \n    if ($connection-&gt;connect_error) {\n        die(\"Connection failed: \" . $connection-&gt;connect_error);\n    }\n    \n    return $connection;\n}\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-KmrTIPwgbK\">3. \u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u4e00\u65e6\u6211\u5011\u6709\u4e86\u9023\u63a5\u51fd\u6578\uff0c\u5c31\u53ef\u4ee5\u6839\u64da\u9700\u8981\u5207\u63db\u6578\u64da\u5eab\u4e86\uff1a<\/p>\n<pre><code>\n$db1Connection = getConnection('db1');\n\/\/ \u57f7\u884c\u4e00\u4e9b\u67e5\u8a62\n$result1 = $db1Connection-&gt;query(\"SELECT * FROM table1\");\n\n$db2Connection = getConnection('db2');\n\/\/ \u57f7\u884c\u5176\u4ed6\u67e5\u8a62\n$result2 = $db2Connection-&gt;query(\"SELECT * FROM table2\");\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-KmrTIPwgbK\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\uff1a<\/strong>\u6bcf\u6b21\u5207\u63db\u6578\u64da\u5eab\u90fd\u9700\u8981\u5efa\u7acb\u65b0\u7684\u9023\u63a5\uff0c\u9019\u53ef\u80fd\u6703\u5f71\u97ff\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u5efa\u8b70\u5728\u9700\u8981\u983b\u7e41\u5207\u63db\u7684\u60c5\u6cc1\u4e0b\uff0c\u8003\u616e\u4f7f\u7528\u9023\u63a5\u6c60\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u52d9\u5fc5\u505a\u597d\u932f\u8aa4\u8655\u7406\uff0c\u4ee5\u9632\u6b62\u56e0\u70ba\u9023\u63a5\u554f\u984c\u5c0e\u81f4\u7684\u61c9\u7528\u5d29\u6f70\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7528\u6236\u540d\u548c\u5bc6\u78bc\u4e0d\u88ab\u786c\u7de8\u78bc\u5728\u4ee3\u78bc\u4e2d\uff0c\u61c9\u4f7f\u7528\u74b0\u5883\u8b8a\u91cf\u6216\u914d\u7f6e\u6587\u4ef6\u4f86\u7ba1\u7406\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KmrTIPwgbK\">\u7e3d\u7d50<\/h2>\n<p>\u5728PHP\u4e2d\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u53ea\u9700\u5b9a\u7fa9\u6578\u64da\u5eab\u914d\u7f6e\u3001\u5275\u5efa\u9023\u63a5\u51fd\u6578\u4e26\u6839\u64da\u9700\u8981\u4f7f\u7528\u5b83\u5011\u3002\u9019\u7a2e\u9748\u6d3b\u6027\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u6839\u64da\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u4f86\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u90fd\u80fd\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728PHP\u4e2d\u8f15\u9b06\u5be6\u73fe\u591a\u6578\u64da\u5eab\u5207\u63db\uff0c\u63d0\u5347\u61c9\u7528\u9748\u6d3b\u6027\u8207\u6548\u7387\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\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-96556","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96556","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=96556"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96556\/revisions"}],"predecessor-version":[{"id":96557,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96556\/revisions\/96557"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}