{"id":96504,"date":"2024-10-20T10:00:25","date_gmt":"2024-10-20T02:00:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96504\/"},"modified":"2024-10-20T10:00:26","modified_gmt":"2024-10-20T02:00:26","slug":"php%e5%92%8cmysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%ae%8c%e7%be%8e%e7%b5%90%e5%90%88-phpmysql%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96504\/","title":{"rendered":"PHP\u548cMySQL\u6578\u64da\u5eab\u7684\u5b8c\u7f8e\u7d50\u5408 (phpmysql\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"php%e5%92%8cmysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%ae%8c%e7%be%8e%e7%b5%90%e5%90%88-phpmysql%e6%95%b8%e6%93%9a%e5%ba%ab-ntSYJNifcL\">PHP\u548cMySQL\u6578\u64da\u5eab\u7684\u5b8c\u7f8e\u7d50\u5408 (phpmysql\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cPHP\u548cMySQL\u7684\u7d50\u5408\u5df2\u6210\u70ba\u4e00\u7a2e\u6a19\u6e96\u7684\u6280\u8853\u5806\u68e7\u3002\u9019\u5169\u8005\u7684\u7d44\u5408\u4e0d\u50c5\u5f37\u5927\u800c\u4e14\u9748\u6d3b\uff0c\u9069\u5408\u5404\u7a2e\u898f\u6a21\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePHP\u548cMySQL\u7684\u7279\u9ede\u3001\u512a\u52e2\u4ee5\u53ca\u5982\u4f55\u6709\u6548\u5730\u5c07\u5b83\u5011\u7d50\u5408\u4f7f\u7528\u3002<\/p>\n<h2 id=\"php%e7%9a%84%e7%89%b9%e9%bb%9e-ntSYJNifcL\">PHP\u7684\u7279\u9ede<\/h2>\n<p>PHP\uff08\u8d85\u6587\u672c\u9810\u8655\u7406\u5668\uff09\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bc\u7db2\u9801\u958b\u767c\u3002\u5176\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6613\u65bc\u5b78\u7fd2\uff1a<\/strong>PHP\u7684\u8a9e\u6cd5\u7c21\u55ae\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\u975e\u5e38\u53cb\u597d\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u6027\uff1a<\/strong>PHP\u53ef\u4ee5\u5728\u591a\u7a2e\u64cd\u4f5c\u7cfb\u7d71\u4e0a\u904b\u884c\uff0c\u5305\u62ecWindows\u3001Linux\u548cmacOS\u3002<\/li>\n<li><strong>\u5f37\u5927\u7684\u793e\u5340\u652f\u6301\uff1a<\/strong>\u64c1\u6709\u5927\u91cf\u7684\u6587\u6a94\u548c\u8cc7\u6e90\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u627e\u5230\u89e3\u6c7a\u65b9\u6848\u3002<\/li>\n<\/ul>\n<h2 id=\"mysql%e7%9a%84%e7%89%b9%e9%bb%9e-ntSYJNifcL\">MySQL\u7684\u7279\u9ede<\/h2>\n<p>MySQL\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\uff1a<\/strong>MySQL\u80fd\u5920\u8655\u7406\u5927\u91cf\u6578\u64da\uff0c\u4e26\u4e14\u5728\u67e5\u8a62\u901f\u5ea6\u4e0a\u8868\u73fe\u512a\u7570\u3002<\/li>\n<li><strong>\u53ef\u9760\u6027\uff1a<\/strong>\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u6027\u5f97\u5230\u4e86\u826f\u597d\u7684\u4fdd\u969c\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\u548c\u8907\u96dc\u7684\u67e5\u8a62\u3002<\/li>\n<\/ul>\n<h2 id=\"php%e8%88%87mysql%e7%9a%84%e7%b5%90%e5%90%88-ntSYJNifcL\">PHP\u8207MySQL\u7684\u7d50\u5408<\/h2>\n<p>PHP\u548cMySQL\u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528PHP\u4f86\u8655\u7406\u7528\u6236\u8acb\u6c42\uff0c\u4e26\u901a\u904eMySQL\u4f86\u5b58\u5132\u548c\u6aa2\u7d22\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u5167\u5bb9\u7ba1\u7406\u7cfb\u7d71\uff08CMS\uff09\uff1a<\/strong>\u5982WordPress\u3001Drupal\u7b49\uff0c\u9019\u4e9b\u7cfb\u7d71\u4f7f\u7528PHP\u548cMySQL\u4f86\u7ba1\u7406\u7db2\u7ad9\u5167\u5bb9\u3002<\/li>\n<li><strong>\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\uff1a<\/strong>\u8a31\u591a\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\u4f7f\u7528\u9019\u4e00\u7d44\u5408\u4f86\u8655\u7406\u7522\u54c1\u4fe1\u606f\u548c\u7528\u6236\u6578\u64da\u3002<\/li>\n<li><strong>\u793e\u4ea4\u5a92\u9ad4\u7db2\u7ad9\uff1a<\/strong>\u793e\u4ea4\u5e73\u53f0\u901a\u5e38\u9700\u8981\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\uff0cPHP\u548cMySQL\u7684\u7d44\u5408\u80fd\u5920\u6eff\u8db3\u9019\u4e00\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%a4%ba%e4%be%8b-ntSYJNifcL\">\u57fa\u672c\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u57f7\u884c\u67e5\u8a62\n$sql = \"SELECT id, name FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u884c\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-ntSYJNifcL\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528PHP\u548cMySQL\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u958b\u767c\u8005\u61c9\u8a72\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\uff1a<\/strong>\u9019\u53ef\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li><strong>\u52a0\u5bc6\u654f\u611f\u6578\u64da\uff1a<\/strong>\u5982\u7528\u6236\u5bc6\u78bc\u61c9\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u9032\u884c\u52a0\u5bc6\u3002<\/li>\n<li><strong>\u5b9a\u671f\u66f4\u65b0\uff1a<\/strong>\u4fdd\u6301PHP\u548cMySQL\u7684\u6700\u65b0\u7248\u672c\uff0c\u4ee5\u7372\u53d6\u6700\u65b0\u7684\u5b89\u5168\u4fee\u88dc\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ntSYJNifcL\">\u7e3d\u7d50<\/h2>\n<p>PHP\u548cMySQL\u7684\u7d50\u5408\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u5404\u7a2e\u7db2\u7d61\u61c9\u7528\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u9019\u4e00\u7d44\u5408\u90fd\u80fd\u5920\u6eff\u8db3\u9700\u6c42\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u900f\u904e\u9019\u4e9b\u5e73\u53f0\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eePHP\u548cMySQL\u7684\u6f5b\u529b\uff0c\u5275\u5efa\u51fa\u8272\u7684\u7db2\u7d61\u61c9\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22PHP\u548cMySQL\u7684\u5b8c\u7f8e\u7d50\u5408\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\uff0c\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u7684\u6700\u4f73\u5be6\u8e10\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-96504","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96504","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=96504"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96504\/revisions"}],"predecessor-version":[{"id":96505,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96504\/revisions\/96505"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}