{"id":183024,"date":"2024-11-10T01:18:21","date_gmt":"2024-11-09T17:18:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183024\/"},"modified":"2024-11-10T01:18:21","modified_gmt":"2024-11-09T17:18:21","slug":"%e5%af%a6%e7%8f%bephp%e4%b8%admysqli%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%b0%81%e8%a3%9d%e6%96%b9%e6%b3%95-mysqli%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e5%b0%81%e8%a3%9d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183024\/","title":{"rendered":"\u5be6\u73fePHP\u4e2dmysqli\u9023\u63a5\u6578\u64da\u5eab\u7684\u5c01\u88dd\u65b9\u6cd5 (mysqli\u9023\u63a5\u6578\u64da\u5eab\u5c01\u88dd)"},"content":{"rendered":"<h1 id=\"%e5%af%a6%e7%8f%bephp%e4%b8%admysqli%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%b0%81%e8%a3%9d%e6%96%b9%e6%b3%95-WmWbRSXiKd\">\u5be6\u73fePHP\u4e2dmysqli\u9023\u63a5\u6578\u64da\u5eab\u7684\u5c01\u88dd\u65b9\u6cd5<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u5176\u4e2d\uff0cmysqli\u64f4\u5c55\u662f\u6700\u5e38\u7528\u7684\u9078\u64c7\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728PHP\u4e2d\u5c01\u88ddmysqli\u9023\u63a5\u6578\u64da\u5eab\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmysqli%ef%bc%9f-WmWbRSXiKd\">\u4ec0\u9ebc\u662fmysqli\uff1f<\/h2>\n<p>mysqli\uff08MySQL Improved\uff09\u662fPHP\u7684\u4e00\u500b\u64f4\u5c55\uff0c\u63d0\u4f9b\u4e86\u5c0dMySQL\u6578\u64da\u5eab\u7684\u6539\u9032\u652f\u6301\u3002\u5b83\u652f\u6301\u9762\u5411\u5c0d\u8c61\u548c\u7a0b\u5e8f\u5316\u7684\u7de8\u7a0b\u98a8\u683c\uff0c\u4e26\u63d0\u4f9b\u4e86\u8a31\u591a\u65b0\u7279\u6027\uff0c\u5982\u9810\u8655\u7406\u8a9e\u53e5\u3001\u4e8b\u52d9\u652f\u6301\u548c\u591a\u8a9e\u53e5\u67e5\u8a62\u7b49\u3002\u4f7f\u7528mysqli\u53ef\u4ee5\u66f4\u5b89\u5168\u5730\u57f7\u884cSQL\u67e5\u8a62\uff0c\u4e26\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/p>\n<h2 id=\"%e5%b0%81%e8%a3%9dmysqli%e9%80%a3%e6%8e%a5%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7-WmWbRSXiKd\">\u5c01\u88ddmysqli\u9023\u63a5\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u91cd\u8907\u7684\u4ee3\u78bc\u6703\u5c0e\u81f4\u7dad\u8b77\u56f0\u96e3\u548c\u932f\u8aa4\u7684\u589e\u52a0\u3002\u901a\u904e\u5c01\u88ddmysqli\u9023\u63a5\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6578\u64da\u5eab\u7684\u9023\u63a5\u908f\u8f2f\u96c6\u4e2d\u7ba1\u7406\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u6b64\u5916\uff0c\u9019\u7a2e\u5c01\u88dd\u9084\u53ef\u4ee5\u65b9\u4fbf\u5730\u9032\u884c\u932f\u8aa4\u8655\u7406\u548c\u65e5\u8a8c\u8a18\u9304\u3002<\/p>\n<h2 id=\"mysqli%e9%80%a3%e6%8e%a5%e7%9a%84%e5%b0%81%e8%a3%9d%e5%af%a6%e7%8f%be-WmWbRSXiKd\">mysqli\u9023\u63a5\u7684\u5c01\u88dd\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684mysqli\u9023\u63a5\u5c01\u88dd\u985e\u7684\u5be6\u73fe\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nclass Database {\n    private $host;\n    private $user;\n    private $password;\n    private $dbname;\n    private $connection;\n\n    public function __construct($host, $user, $password, $dbname) {\n        $this-&gt;host = $host;\n        $this-&gt;user = $user;\n        $this-&gt;password = $password;\n        $this-&gt;dbname = $dbname;\n        $this-&gt;connect();\n    }\n\n    private function connect() {\n        $this-&gt;connection = new mysqli($this-&gt;host, $this-&gt;user, $this-&gt;password, $this-&gt;dbname);\n\n        if ($this-&gt;connection-&gt;connect_error) {\n            die(\"\u9023\u63a5\u5931\u6557: \" . $this-&gt;connection-&gt;connect_error);\n        }\n    }\n\n    public function query($sql) {\n        $result = $this-&gt;connection-&gt;query($sql);\n        if (!$result) {\n            die(\"\u67e5\u8a62\u5931\u6557: \" . $this-&gt;connection-&gt;error);\n        }\n        return $result;\n    }\n\n    public function close() {\n        $this-&gt;connection-&gt;close();\n    }\n}\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b-WmWbRSXiKd\">\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u5c01\u88dd\u985e\u4f86\u9023\u63a5\u6578\u64da\u5eab\u548c\u57f7\u884c\u67e5\u8a62\u975e\u5e38\u7c21\u55ae\uff1a<\/p>\n<pre><code>\n$db = new Database('localhost', 'username', 'password', 'database_name');\n$result = $db-&gt;query('SELECT * FROM users');\n\nwhile ($row = $result-&gt;fetch_assoc()) {\n    echo \"\u7528\u6236\u540d: \" . $row['username'] . \"<br>\";\n}\n\n$db-&gt;close();\n<\/code><\/pre>\n<h2 id=\"%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86%e8%88%87%e6%97%a5%e8%aa%8c%e8%a8%98%e9%8c%84-WmWbRSXiKd\">\u932f\u8aa4\u8655\u7406\u8207\u65e5\u8a8c\u8a18\u9304<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u932f\u8aa4\u8655\u7406\u548c\u65e5\u8a8c\u8a18\u9304\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u5728\u5c01\u88dd\u985e\u4e2d\u6dfb\u52a0\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u4e26\u5c07\u932f\u8aa4\u4fe1\u606f\u8a18\u9304\u5230\u65e5\u8a8c\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u7684\u6392\u67e5\u548c\u5206\u6790\u3002<\/p>\n<pre><code>\npublic function query($sql) {\n    $result = $this-&gt;connection-&gt;query($sql);\n    if (!$result) {\n        error_log(\"\u67e5\u8a62\u5931\u6557: \" . $this-&gt;connection-&gt;error);\n        return false;\n    }\n    return $result;\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WmWbRSXiKd\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u5c01\u88ddmysqli\u9023\u63a5\u6578\u64da\u5eab\u7684\u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u9084\u80fd\u6709\u6548\u5730\u8655\u7406\u932f\u8aa4\u548c\u8a18\u9304\u65e5\u8a8c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u6280\u5de7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u4f86\u652f\u6301\u60a8\u7684\u7db2\u7ad9\u6216\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728PHP\u4e2d\u5be6\u73femysqli\u9023\u63a5\u6578\u64da\u5eab\u7684\u5c01\u88dd\u65b9\u6cd5\uff0c\u63d0\u5347\u4ee3\u78bc\u91cd\u7528\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-183024","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183024","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=183024"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183024\/revisions"}],"predecessor-version":[{"id":183025,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183024\/revisions\/183025"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}