{"id":96502,"date":"2024-10-20T09:59:18","date_gmt":"2024-10-20T01:59:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96502\/"},"modified":"2024-10-20T09:59:18","modified_gmt":"2024-10-20T01:59:18","slug":"php%e5%92%8cmysql%e5%af%a6%e7%8f%be%e9%95%b7%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-php-%e6%95%b8%e6%93%9a%e5%ba%ab-%e9%95%b7%e9%80%a3%e6%8e%a5-mysql","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96502\/","title":{"rendered":"PHP\u548cMySQL\u5be6\u73fe\u9577\u9023\u63a5\u6578\u64da\u5eab\u64cd\u4f5c (php \u6578\u64da\u5eab \u9577\u9023\u63a5 mysql)"},"content":{"rendered":"<h1 id=\"php%e5%92%8cmysql%e5%af%a6%e7%8f%be%e9%95%b7%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-nRDsgzTfqp\">PHP\u548cMySQL\u5be6\u73fe\u9577\u9023\u63a5\u6578\u64da\u5eab\u64cd\u4f5c<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002PHP\u548cMySQL\u662f\u6700\u5e38\u7528\u7684\u7d44\u5408\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u548cMySQL\u5be6\u73fe\u9577\u9023\u63a5\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u5206\u6790\u5176\u512a\u52e2\u548c\u5be6\u73fe\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%95%b7%e9%80%a3%e6%8e%a5%ef%bc%9f-nRDsgzTfqp\">\u4ec0\u9ebc\u662f\u9577\u9023\u63a5\uff1f<\/h2>\n<p>\u9577\u9023\u63a5\uff08Persistent Connection\uff09\u662f\u6307\u5728\u5ba2\u6236\u7aef\u548c\u6578\u64da\u5eab\u4e4b\u9593\u4fdd\u6301\u6301\u4e45\u7684\u9023\u63a5\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u8acb\u6c42\u90fd\u91cd\u65b0\u5efa\u7acb\u9023\u63a5\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u9023\u63a5\u7684\u958b\u92b7\uff0c\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<h2 id=\"%e9%95%b7%e9%80%a3%e6%8e%a5%e7%9a%84%e5%84%aa%e5%8b%a2-nRDsgzTfqp\">\u9577\u9023\u63a5\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u6e1b\u5c11\u5ef6\u9072\uff1a<\/strong>\u6bcf\u6b21\u5efa\u7acb\u65b0\u9023\u63a5\u90fd\u9700\u8981\u6642\u9593\uff0c\u9577\u9023\u63a5\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u9019\u7a2e\u5ef6\u9072\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u5728\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u4e2d\uff0c\u9577\u9023\u63a5\u53ef\u4ee5\u6e1b\u5c11\u8cc7\u6e90\u7684\u6d88\u8017\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u66f4\u597d\u7684\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u9577\u9023\u63a5\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u8cc7\u6e90\uff0c\u7279\u5225\u662f\u5728\u591a\u7528\u6236\u74b0\u5883\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e5%af%a6%e7%8f%be%e9%95%b7%e9%80%a3%e6%8e%a5-nRDsgzTfqp\">\u5982\u4f55\u5728PHP\u4e2d\u5be6\u73fe\u9577\u9023\u63a5<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528mysqli\u64f4\u5c55\u4f86\u5be6\u73fe\u9577\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u5be6\u73fe\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8mysqli_connect%e5%87%bd%e6%95%b8-nRDsgzTfqp\">1. \u4f7f\u7528mysqli_connect()\u51fd\u6578<\/h3>\n<pre><code>\n$host = 'localhost';\n$user = 'username';\n$password = 'password';\n$database = 'database_name';\n\n\/\/ \u4f7f\u7528\u6301\u4e45\u9023\u63a5\n$connection = mysqli_connect('p:' . $host, $user, $password, $database);\n\nif (!$connection) {\n    die('\u9023\u63a5\u5931\u6557: ' . mysqli_connect_error());\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u901a\u904e\u5728\u4e3b\u6a5f\u540d\u7a31\u524d\u52a0\u4e0a\u201cp:\u201d\u4f86\u5be6\u73fe\u6301\u4e45\u9023\u63a5\u3002\u9019\u6a23\uff0c\u9023\u63a5\u5c07\u4e0d\u6703\u5728\u6bcf\u6b21\u8acb\u6c42\u5f8c\u95dc\u9589\u3002<\/p>\n<h3 id=\"2-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-nRDsgzTfqp\">2. \u57f7\u884c\u67e5\u8a62<\/h3>\n<pre><code>\n$query = \"SELECT * FROM users\";\n$result = mysqli_query($connection, $query);\n\nif ($result) {\n    while ($row = mysqli_fetch_assoc($result)) {\n        echo '\u7528\u6236\u540d: ' . $row['username'] . '<br>';\n    }\n    mysqli_free_result($result);\n} else {\n    echo '\u67e5\u8a62\u5931\u6557: ' . mysqli_error($connection);\n}\n<\/code><\/pre>\n<h3 id=\"3-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-nRDsgzTfqp\">3. \u95dc\u9589\u9023\u63a5<\/h3>\n<p>\u7531\u65bc\u4f7f\u7528\u4e86\u9577\u9023\u63a5\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u64cd\u4f5c\u5f8c\u95dc\u9589\u9023\u63a5\u3002\u7576PHP\u8173\u672c\u57f7\u884c\u5b8c\u7562\u5f8c\uff0c\u9023\u63a5\u6703\u81ea\u52d5\u4fdd\u6301\u958b\u555f\u72c0\u614b\uff0c\u76f4\u5230\u9054\u5230\u6700\u5927\u9023\u63a5\u6578\u6216\u8d85\u6642\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-nRDsgzTfqp\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u96d6\u7136\u9577\u9023\u63a5\u6709\u5176\u512a\u52e2\uff0c\u4f46\u4e5f\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u9577\u9023\u63a5\u6703\u4f54\u7528\u670d\u52d9\u5668\u8cc7\u6e90\uff0c\u56e0\u6b64\u9700\u8981\u5408\u7406\u914d\u7f6e\u6700\u5927\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>\u8d85\u6642\u8a2d\u7f6e\uff1a<\/strong>\u61c9\u8a2d\u7f6e\u5408\u7406\u7684\u8d85\u6642\u6642\u9593\uff0c\u4ee5\u9632\u6b62\u9023\u63a5\u9577\u6642\u9593\u4f54\u7528\u8cc7\u6e90\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5728\u4f7f\u7528\u9577\u9023\u63a5\u6642\uff0c\u61c9\u52a0\u5f37\u932f\u8aa4\u8655\u7406\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-nRDsgzTfqp\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP\u548cMySQL\u5be6\u73fe\u9577\u9023\u63a5\u6578\u64da\u5eab\u64cd\u4f5c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u7ba1\u7406\uff0c\u9577\u9023\u63a5\u80fd\u5920\u70ba\u9ad8\u6d41\u91cf\u7684\u7db2\u7ad9\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u9019\u5c07\u70ba\u60a8\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528PHP\u548cMySQL\u5be6\u73fe\u9577\u9023\u63a5\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6027\u80fd\u548c\u6548\u7387\uff0c\u512a\u5316\u6578\u64da\u8655\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-96502","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96502","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=96502"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96502\/revisions"}],"predecessor-version":[{"id":96503,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96502\/revisions\/96503"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}