{"id":199751,"date":"2025-05-06T08:37:11","date_gmt":"2025-05-06T00:37:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199751\/"},"modified":"2025-05-06T08:37:11","modified_gmt":"2025-05-06T00:37:11","slug":"php%e5%92%8cmysql%e7%9a%84%e5%8d%8f%e4%bd%9c%ef%bc%9a%e6%8e%a2%e7%a9%b6%e4%b8%a4%e8%80%85%e4%b8%ba%e4%bd%95%e5%a6%82%e6%ad%a4%e5%8d%8f%e8%b0%83%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199751\/","title":{"rendered":"PHP\u548cMySQL\u7684\u534f\u4f5c\uff1a\u63a2\u7a76\u4e24\u8005\u4e3a\u4f55\u5982\u6b64\u534f\u8c03\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u548cMySQL\u7684\u534f\u4f5c\uff1a\u63a2\u7a76\u4e24\u8005\u4e3a\u4f55\u5982\u6b64\u534f\u8c03\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP\u548cMySQL\u7684\u534f\u4f5c\uff1a\u63a2\u7a76\u4e24\u8005\u4e3a\u4f55\u5982\u6b64\u534f\u8c03\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>PHP\u548cMySQL\u4f5c\u4e3aWeb\u5f00\u53d1\u4e2d\u7684\u9ec4\u91d1\u642d\u6863\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u9ed8\u5951\u914d\u5408\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u5feb\u901f\u5730\u521b\u5efa\u5f3a\u5927\u7684\u52a8\u6001\u7f51\u7ad9\u548cWeb\u5e94\u7528\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u7a76PHP\u548cMySQL\u4e4b\u95f4\u7684\u9ed8\u5951\uff0c\u5e76\u5206\u6790\u4e3a\u4f55\u5b83\u4eec\u5982\u6b64\u914d\u5408\u9ed8\u5951\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4f1a\u7ed3\u5408\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u9610\u8ff0\u5b83\u4eec\u4e4b\u95f4\u7684\u534f\u4f5c\u5173\u7cfb\u3002<\/p>\n<p>PHP\u662f\u4e00\u79cd\u6d41\u884c\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5f00\u53d1\u3002MySQL\u5219\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002PHP\u548cMySQL\u4e4b\u95f4\u7684\u914d\u5408\u53ef\u4ee5\u8bf4\u662f\u5929\u4f5c\u4e4b\u5408\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u8fde\u63a5<br \/>\u5728Web\u5f00\u53d1\u4e2d\uff0cPHP\u901a\u5e38\u88ab\u7528\u6765\u5904\u7406\u52a8\u6001\u5185\u5bb9\u548c\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002PHP\u4e0eMySQL\u4e4b\u95f4\u7684\u9ed8\u5951\u8868\u73b0\u5728\u5b83\u4eec\u4e4b\u95f4\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7b80\u6613\u6027\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528PHP\u4e2d\u7684mysqli\u6216PDO\u7b49\u6269\u5c55\u6765\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PHP\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff1a<\/p>\n<pre>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"mydatabase\";\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n} else {\n    echo \"\u8fde\u63a5\u6210\u529f\";\n}\n?&gt;<\/pre>\n<ol>\n<li>\u6570\u636e\u5e93\u67e5\u8be2<br \/>PHP\u548cMySQL\u4e4b\u95f4\u7684\u9ed8\u5951\u5728\u4e8e\u5b83\u4eec\u80fd\u591f\u65e0\u7f1d\u5730\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528PHP\u4e2d\u7684SQL\u8bed\u53e5\u6765\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u7f51\u9875\u3002\u8fd9\u79cd\u914d\u5408\u4f7f\u5f97\u52a8\u6001\u7f51\u9875\u7684\u5f00\u53d1\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PHP\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u6267\u884cSELECT\u67e5\u8be2\u5e76\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>&lt;?php\n$sql = \"SELECT id, name, email FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"ID: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \" - Email: \" . $row[\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7ed3\u679c\";\n}\n\n$conn-&gt;close();\n?&gt;<\/pre>\n<ol>\n<li>\u6570\u636e\u5e93\u66f4\u65b0\u548c\u5220\u9664<br \/>\u9664\u4e86\u67e5\u8be2\u64cd\u4f5c\uff0cPHP\u548cMySQL\u4e4b\u95f4\u7684\u9ed8\u5951\u8fd8\u4f53\u73b0\u5728\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u8bb0\u5f55\u4e0a\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528PHP\u4ee3\u7801\u5411\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u65b0\u6570\u636e\u3001\u66f4\u65b0\u6570\u636e\u8bb0\u5f55\u6216\u5220\u9664\u6570\u636e\u8bb0\u5f55\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u6570\u636e\u7684\u589e\u5220\u6539\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PHP\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u6267\u884cUPDATE\u548cDELETE\u64cd\u4f5c\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u66f4\u65b0\u6570\u636e\n$sql = \"UPDATE users SET email='newemail@example.com' WHERE id=1\";\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u6570\u636e\u66f4\u65b0\u6210\u529f\";\n} else {\n    echo \"\u66f4\u65b0\u51fa\u9519: \" . $conn-&gt;error;\n}\n\n\/\/ \u5220\u9664\u6570\u636e\n$sql = \"DELETE FROM users WHERE id=2\";\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u6570\u636e\u5220\u9664\u6210\u529f\";\n} else {\n    echo \"\u5220\u9664\u51fa\u9519: \" . $conn-&gt;error;\n}\n\n$conn-&gt;close();\n?&gt;<\/pre>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cPHP\u548cMySQL\u4e4b\u95f4\u7684\u9ed8\u5951\u914d\u5408\u4f53\u73b0\u5728\u5b83\u4eec\u7684\u4e92\u8865\u6027\u548c\u9ad8\u6548\u6027\u4e0a\u3002PHP\u4f5c\u4e3a\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u53ef\u4ee5\u5904\u7406\u52a8\u6001\u5185\u5bb9\uff0c\u800cMySQL\u4f5c\u4e3a\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u5219\u8d1f\u8d23\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002\u5b83\u4eec\u4e4b\u95f4\u914d\u5408\u9ed8\u5951\uff0c\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u5feb\u901f\u5730\u6784\u5efa\u529f\u80fd\u5f3a\u5927\u7684Web\u5e94\u7528\u3002\u8fd9\u79cd\u9ed8\u5951\u5173\u7cfb\u4e3aWeb\u5f00\u53d1\u5e26\u6765\u4e86\u4fbf\u5229\u548c\u9ad8\u6548\uff0c\u6210\u4e3a\u4f17\u591a\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u6280\u672f\u7ec4\u5408\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP\u548cMySQL\u7684\u534f\u4f5c\uff1a\u63a2\u7a76\u4e24\u8005\u4e3a\u4f55\u5982\u6b64\u534f\u8c03\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u548cMySQL\u7684\u534f\u4f5c\uff1a\u63a2\u7a76\u4e24&#46;&#46;&#46;<\/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-199751","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199751","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=199751"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199751\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}