{"id":168807,"date":"2024-11-06T16:23:47","date_gmt":"2024-11-06T08:23:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168807\/"},"modified":"2024-11-06T16:23:48","modified_gmt":"2024-11-06T08:23:48","slug":"tp-%e6%a1%86%e6%9e%b6%e4%b8%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%95%99%e7%a8%8b-%e6%80%8e%e9%ba%bc%e5%9c%a8-tp-%e4%b8%ad%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168807\/","title":{"rendered":"TP \u6846\u67b6\u4e0b\u6578\u64da\u5eab\u9023\u63a5\u6559\u7a0b (\u600e\u9ebc\u5728 tp \u4e2d\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"tp-%e6%a1%86%e6%9e%b6%e4%b8%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%95%99%e7%a8%8b-%e6%80%8e%e9%ba%bc%e5%9c%a8-tp-%e4%b8%ad%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-nwgxrfcYva\">TP \u6846\u67b6\u4e0b\u6578\u64da\u5eab\u9023\u63a5\u6559\u7a0b (\u600e\u9ebc\u5728 tp \u4e2d\u9023\u63a5\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u5c0d\u65bc\u4f7f\u7528 TP\uff08ThinkPHP\uff09\u6846\u67b6\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u6b63\u78ba\u5730\u9023\u63a5\u6578\u64da\u5eab\u662f\u69cb\u5efa\u61c9\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 TP \u6846\u67b6\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-nwgxrfcYva\">1. \u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u60a8\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>PHP \u7248\u672c 7.1 \u6216\u4ee5\u4e0a<\/li>\n<li>MySQL \u6216\u5176\u4ed6\u652f\u6301\u7684\u6578\u64da\u5eab<\/li>\n<li>ThinkPHP \u6846\u67b6<\/li>\n<\/ul>\n<h2 id=\"2-%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-nwgxrfcYva\">2. \u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728 TP \u6846\u67b6\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u65bc <code>application\/database.php<\/code>\u3002\u60a8\u9700\u8981\u6839\u64da\u81ea\u5df1\u7684\u6578\u64da\u5eab\u4fe1\u606f\u9032\u884c\u76f8\u61c9\u7684\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u6578\u64da\u5eab\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nreturn [\n    'type'     =&gt; 'mysql', \/\/ \u6578\u64da\u5eab\u985e\u578b\n    'hostname' =&gt; '127.0.0.1', \/\/ \u6578\u64da\u5eab\u4e3b\u6a5f\n    'database' =&gt; 'your_database_name', \/\/ \u6578\u64da\u5eab\u540d\u7a31\n    'username' =&gt; 'your_username', \/\/ \u6578\u64da\u5eab\u7528\u6236\u540d\n    'password' =&gt; 'your_password', \/\/ \u6578\u64da\u5eab\u5bc6\u78bc\n    'hostport' =&gt; '3306', \/\/ \u6578\u64da\u5eab\u7aef\u53e3\n    'charset'  =&gt; 'utf8', \/\/ \u5b57\u7b26\u96c6\n    'prefix'   =&gt; 'tp_', \/\/ \u8868\u524d\u7db4\n];\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0c\u60a8\u9700\u8981\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63db <code>your_database_name<\/code>\u3001<code>your_username<\/code> \u548c <code>your_password<\/code>\u3002<\/p>\n<h2 id=\"3-%e6%b8%ac%e8%a9%a6%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-nwgxrfcYva\">3. \u6e2c\u8a66\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u914d\u7f6e\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u4ee3\u78bc\u6e2c\u8a66\u6578\u64da\u5eab\u9023\u63a5\u662f\u5426\u6210\u529f\u3002\u5728 <code>application\/index\/controller\/Index.php<\/code> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\nnamespace appindexcontroller;\n\nuse thinkController;\nuse thinkDb;\n\nclass Index extends Controller\n{\n    public function index()\n    {\n        try {\n            \/\/ \u6e2c\u8a66\u6578\u64da\u5eab\u9023\u63a5\n            $result = Db::query('SELECT VERSION() AS version');\n            return json($result);\n        } catch (Exception $e) {\n            return json(['error' =&gt; $e-&gt;getMessage()]);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u7576\u60a8\u8a2a\u554f\u9996\u9801\u6642\uff0c\u61c9\u8a72\u80fd\u5920\u770b\u5230\u6578\u64da\u5eab\u7248\u672c\u4fe1\u606f\uff0c\u9019\u8868\u793a\u9023\u63a5\u6210\u529f\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-nwgxrfcYva\">4. \u4f7f\u7528\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u6578\u64da\u5eab\uff0c\u60a8\u5c31\u53ef\u4ee5\u4f7f\u7528 TP \u6846\u67b6\u63d0\u4f9b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u65b9\u6cd5\u9032\u884c CRUD\uff08\u5275\u5efa\u3001\u8b80\u53d6\u3001\u66f4\u65b0\u3001\u522a\u9664\uff09\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"4-1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a-nwgxrfcYva\">4.1 \u5275\u5efa\u6578\u64da<\/h3>\n<pre><code>\nDb::name('users')-&gt;insert(['name' =&gt; 'John Doe', 'email' =&gt; 'john@example.com']);\n<\/code><\/pre>\n<h3 id=\"4-2-%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a-nwgxrfcYva\">4.2 \u8b80\u53d6\u6578\u64da<\/h3>\n<pre><code>\n$user = Db::name('users')-&gt;where('id', 1)-&gt;find();\n<\/code><\/pre>\n<h3 id=\"4-3-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-nwgxrfcYva\">4.3 \u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>\nDb::name('users')-&gt;where('id', 1)-&gt;update(['email' =&gt; 'john.doe@example.com']);\n<\/code><\/pre>\n<h3 id=\"4-4-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-nwgxrfcYva\">4.4 \u522a\u9664\u6578\u64da<\/h3>\n<pre><code>\nDb::name('users')-&gt;where('id', 1)-&gt;delete();\n<\/code><\/pre>\n<h2 id=\"5-%e5%b8%b8%e8%a6%8b%e5%95%8f%e9%a1%8c-nwgxrfcYva\">5. \u5e38\u898b\u554f\u984c<\/h2>\n<ul>\n<li><strong>\u5982\u4f55\u8655\u7406\u6578\u64da\u5eab\u9023\u63a5\u932f\u8aa4\uff1f<\/strong> \u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u4e3b\u6a5f\u540d\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li><strong>\u5982\u4f55\u66f4\u6539\u6578\u64da\u5eab\u985e\u578b\uff1f<\/strong> \u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u4fee\u6539 <code>'type'<\/code> \u7684\u503c\uff0c\u4f8b\u5982 <code>'pgsql'<\/code> \u6216 <code>'sqlite'<\/code>\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-nwgxrfcYva\">\u7e3d\u7d50<\/h2>\n<p>\u5728 TP \u6846\u67b6\u4e2d\u9023\u63a5\u6578\u64da\u5eab\u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\uff0c\u53ea\u9700\u6b63\u78ba\u914d\u7f6e\u6578\u64da\u5eab\u4fe1\u606f\u4e26\u4f7f\u7528\u6846\u67b6\u63d0\u4f9b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u65b9\u6cd5\u5373\u53ef\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u7684\u5be6\u73fe\uff0c\u800c\u4e0d\u5fc5\u904e\u591a\u64d4\u5fc3\u5e95\u5c64\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684 TP \u61c9\u7528\u7a0b\u5e8f\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u9805\u76ee\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 TP \u6846\u67b6\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u638c\u63e1\u914d\u7f6e\u6b65\u9a5f\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u4ea4\u4e92\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-168807","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168807","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=168807"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168807\/revisions"}],"predecessor-version":[{"id":168808,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168807\/revisions\/168808"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}