{"id":59591,"date":"2024-10-12T23:15:53","date_gmt":"2024-10-12T15:15:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59591\/"},"modified":"2024-10-12T23:15:53","modified_gmt":"2024-10-12T15:15:53","slug":"ci%e6%a1%86%e6%9e%b6%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e5%8a%a0%e8%bc%89-ci%e6%a1%86%e6%9e%b6%e5%8a%a0%e8%bc%89%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59591\/","title":{"rendered":"CI\u6846\u67b6\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u52a0\u8f09 (ci\u6846\u67b6\u52a0\u8f09\u6578\u64da\u5eab\u9023\u63a5)"},"content":{"rendered":"<h1 id=\"ci%e6%a1%86%e6%9e%b6%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e5%8a%a0%e8%bc%89-hITZVRsLVy\">CI\u6846\u67b6\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u52a0\u8f09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002CodeIgniter\uff08CI\uff09\u6846\u67b6\u4f5c\u70ba\u4e00\u500b\u8f15\u91cf\u7d1a\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728CI\u6846\u67b6\u4e2d\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u52a0\u8f09\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"1-codeigniter%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-hITZVRsLVy\">1. CodeIgniter\u7684\u6578\u64da\u5eab\u914d\u7f6e<\/h2>\n<p>\u5728\u4f7f\u7528CI\u6846\u67b6\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3002\u9019\u901a\u5e38\u5728\u61c9\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u3002\u4ee5\u4e0b\u662f\u914d\u7f6e\u6578\u64da\u5eab\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>application\/config\/database.php<\/code><\/pre>\n<p>\u5728\u9019\u500b\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u6578\u64da\u5eab\u7684\u914d\u7f6e\u9078\u9805\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u914d\u7f6e\uff1a<\/p>\n<pre><code>$db['default'] = array(\n    'dsn'   =&gt; '',\n    'hostname' =&gt; 'localhost',\n    'username' =&gt; 'your_username',\n    'password' =&gt; 'your_password',\n    'database' =&gt; 'your_database',\n    'dbdriver' =&gt; 'mysqli',\n    'dbprefix' =&gt; '',\n    'pconnect' =&gt; FALSE,\n    'db_debug' =&gt; (ENVIRONMENT !== 'production'),\n    'cache_on' =&gt; FALSE,\n    'cachedir' =&gt; '',\n    'char_set' =&gt; 'utf8',\n    'dbcollat' =&gt; 'utf8_general_ci',\n    'swap_pre' =&gt; '',\n    'encrypt' =&gt; FALSE,\n    'compress' =&gt; FALSE,\n    'stricton' =&gt; FALSE,\n    'failover' =&gt; array(),\n    'save_queries' =&gt; TRUE\n);<\/code><\/pre>\n<h2 id=\"2-%e5%8a%a0%e8%bc%89%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-hITZVRsLVy\">2. \u52a0\u8f09\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728CI\u6846\u67b6\u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u7684\u52a0\u8f09\u975e\u5e38\u7c21\u55ae\u3002\u60a8\u53ea\u9700\u5728\u63a7\u5236\u5668\u4e2d\u52a0\u8f09\u6578\u64da\u5eab\u985e\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u63a7\u5236\u5668\u4e2d\u52a0\u8f09\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>class Welcome extends CI_Controller {\n    \n    public function __construct() {\n        parent::__construct();\n        $this-&gt;load-&gt;database(); \/\/ \u52a0\u8f09\u6578\u64da\u5eab\n    }\n\n    public function index() {\n        $query = $this-&gt;db-&gt;get('your_table'); \/\/ \u7372\u53d6\u6578\u64da\n        $data['results'] = $query-&gt;result();\n        $this-&gt;load-&gt;view('welcome_message', $data);\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5728\u63a7\u5236\u5668\u7684\u69cb\u9020\u51fd\u6578\u4e2d\u52a0\u8f09\u4e86\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u5728index\u65b9\u6cd5\u4e2d\u57f7\u884c\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u67e5\u8a62\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8active-record%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-hITZVRsLVy\">3. \u4f7f\u7528Active Record\u9032\u884c\u6578\u64da\u64cd\u4f5c<\/h2>\n<p>CI\u6846\u67b6\u63d0\u4f9b\u4e86Active Record\u985e\u4f86\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u9019\u4f7f\u5f97\u67e5\u8a62\u548c\u6578\u64da\u64cd\u4f5c\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6578\u64da\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"3-1-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-hITZVRsLVy\">3.1 \u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>$data = array(\n    'title' =&gt; 'My Title',\n    'name' =&gt; 'My Name',\n    'date' =&gt; 'My Date'\n);\n\n$this-&gt;db-&gt;insert('your_table', $data); \/\/ \u63d2\u5165\u6578\u64da<\/code><\/pre>\n<h3 id=\"3-2-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-hITZVRsLVy\">3.2 \u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>$data = array(\n    'title' =&gt; 'Updated Title'\n);\n\n$this-&gt;db-&gt;where('id', 1);\n$this-&gt;db-&gt;update('your_table', $data); \/\/ \u66f4\u65b0\u6578\u64da<\/code><\/pre>\n<h3 id=\"3-3-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-hITZVRsLVy\">3.3 \u522a\u9664\u6578\u64da<\/h3>\n<pre><code>$this-&gt;db-&gt;where('id', 1);\n$this-&gt;db-&gt;delete('your_table'); \/\/ \u522a\u9664\u6578\u64da<\/code><\/pre>\n<h2 id=\"4-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-hITZVRsLVy\">4. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002CI\u6846\u67b6\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u6aa2\u67e5\u6578\u64da\u5eab\u932f\u8aa4\uff1a<\/p>\n<pre><code>if ($this-&gt;db-&gt;error()['code'] != 0) {\n    \/\/ \u8655\u7406\u932f\u8aa4\n    log_message('error', 'Database error: ' . $this-&gt;db-&gt;error()['message']);\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-hITZVRsLVy\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u7684\u4ecb\u7d39\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5728CodeIgniter\u6846\u67b6\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u52a0\u8f09\u662f\u76f8\u5c0d\u7c21\u55ae\u7684\u3002\u5f9e\u914d\u7f6e\u6578\u64da\u5eab\u5230\u4f7f\u7528Active Record\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0cCI\u6846\u67b6\u63d0\u4f9b\u4e86\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u6578\u64da\u5eab\u7ba1\u7406\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0cCI\u6846\u67b6\u7121\u7591\u662f\u4e00\u500b\u7406\u60f3\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u60a8\u7684\u6578\u64da\u5eab\u548c\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u4f7f\u7528CI\u6846\u67b6\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u52a0\u8f09\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\uff0c\u63d0\u5347\u6548\u7387\uff0c\u8b93\u6578\u64da\u64cd\u4f5c\u66f4\u4fbf\u6377\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-59591","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59591","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=59591"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59591\/revisions"}],"predecessor-version":[{"id":59592,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59591\/revisions\/59592"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}