{"id":48556,"date":"2024-12-02T16:13:39","date_gmt":"2024-12-02T08:13:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48556\/"},"modified":"2024-12-02T16:13:39","modified_gmt":"2024-12-02T08:13:39","slug":"php%e5%87%bd%e6%95%b0%e4%b8%8e%e6%95%b0%e6%8d%ae%e5%ba%93%e4%ba%a4%e4%ba%92%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48556\/","title":{"rendered":"php\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aphp\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u4f7f\u7528 PHP \u51fd\u6570\u64cd\u4f5c MySQL \u6570\u636e\u5e93\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528 mysqli_connect() \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u4f7f\u7528 mysqli_query() \u6267\u884c SQL \u67e5\u8be2\u4f7f\u7528 mysqli_query() \u63d2\u5165\u6570\u636e\u4f7f\u7528 mysqli_query() \u66f4\u65b0\u6570\u636e\u4f7f\u7528 mysqli_query() \u5220\u9664\u6570\u636e<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730080246671eedf6b2679.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357\u63d2\u56fe\" alt=\"php\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 PHP \u51fd\u6570\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u51fd\u6570\u6765\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u6267\u884c\u5e38\u89c1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5305\u62ec\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u3002<\/p>\n<p><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528 <code>mysqli_connect()<\/code> \u51fd\u6570\u5efa\u7acb\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<pre>$servername = \"localhost\";\n$username = \"root\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u521b\u5efa\u4e00\u4e2a MySQL \u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f\nif (!$conn) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . mysqli_connect_error());\n}<\/pre>\n<p><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528 <code>mysqli_query()<\/code> \u51fd\u6570\u6267\u884c SQL \u67e5\u8be2\uff1a<\/p>\n<pre>\/\/ \u51c6\u5907\u8981\u6267\u884c\u7684\u67e5\u8be2\n$query = \"SELECT * FROM customers\";\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$result = mysqli_query($conn, $query);\n\n\/\/ \u68c0\u67e5\u67e5\u8be2\u662f\u5426\u6210\u529f\nif (!$result) {\n    die(\"\u67e5\u8be2\u5931\u8d25: \" . mysqli_error($conn));\n}<\/pre>\n<p><strong>\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 <code>mysqli_query()<\/code> \u51fd\u6570\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<pre>\/\/ \u51c6\u5907\u8981\u6267\u884c\u7684\u67e5\u8be2\n$query = \"INSERT INTO customers (name, email) VALUES ('John Doe', 'john.doe@example.com')\";\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$result = mysqli_query($conn, $query);\n\n\/\/ \u68c0\u67e5\u67e5\u8be2\u662f\u5426\u6210\u529f\nif (!$result) {\n    die(\"\u63d2\u5165\u5931\u8d25: \" . mysqli_error($conn));\n}<\/pre>\n<p><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 <code>mysqli_query()<\/code> \u51fd\u6570\u66f4\u65b0\u6570\u636e\uff1a<\/p>\n<pre>\/\/ \u51c6\u5907\u8981\u6267\u884c\u7684\u67e5\u8be2\n$query = \"UPDATE customers SET name = 'Jane Doe' WHERE id = 1\";\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$result = mysqli_query($conn, $query);\n\n\/\/ \u68c0\u67e5\u67e5\u8be2\u662f\u5426\u6210\u529f\nif (!$result) {\n    die(\"\u66f4\u65b0\u5931\u8d25: \" . mysqli_error($conn));\n}<\/pre>\n<p><strong>\u5220\u9664\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 <code>mysqli_query()<\/code> \u51fd\u6570\u5220\u9664\u6570\u636e\uff1a<\/p>\n<pre>\/\/ \u51c6\u5907\u8981\u6267\u884c\u7684\u67e5\u8be2\n$query = \"DELETE FROM customers WHERE id = 2\";\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$result = mysqli_query($conn, $query);\n\n\/\/ \u68c0\u67e5\u67e5\u8be2\u662f\u5426\u6210\u529f\nif (!$result) {\n    die(\"\u5220\u9664\u5931\u8d25: \" . mysqli_error($conn));\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 PHP \u51fd\u6570\u64cd\u4f5c MySQL \u6570\u636e\u5e93\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u8fde\u63a5\u5230\u6570\u636e\u5e93\n$conn = mysqli_connect(\"localhost\", \"root\", \"password\", \"database_name\");\n\n\/\/ \u63d2\u5165\u65b0\u8bb0\u5f55\n$query = \"INSERT INTO customers (name, email) VALUES ('John Doe', 'john.doe@example.com')\";\nmysqli_query($conn, $query);\n\n\/\/ \u8bfb\u53d6\u6240\u6709\u8bb0\u5f55\n$query = \"SELECT * FROM customers\";\n$result = mysqli_query($conn, $query);\n\n\/\/ \u904d\u5386\u7ed3\u679c\u5e76\u6253\u5370\u6bcf\u4e2a\u8bb0\u5f55\nwhile ($row = mysqli_fetch_assoc($result)) {\n    echo \"ID: \" . $row[\"id\"] . \"&lt;br&gt;\";\n    echo \"Name: \" . $row[\"name\"] . \"&lt;br&gt;\";\n    echo \"Email: \" . $row[\"email\"] . \"&lt;br&gt;&lt;br&gt;\";\n}\n\n\/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\nmysqli_close($conn);<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aphp\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357 \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aphp\u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u4f7f\u7528 PHP \u51fd\u6570\u64cd\u4f5c MySQL \u6570\u636e\u5e93\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528 mysqli_connect() \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u4f7f\u7528 mysqli_query() \u6267\u884c SQL \u67e5\u8be2\u4f7f\u7528 mysqli_query() \u63d2\u5165\u6570\u636e\u4f7f\u7528 mysqli_query() \u66f4\u65b0\u6570\u636e\u4f7f\u7528 mysqli_query() \u5220\u9664\u6570\u636e \u4f7f\u7528 PHP \u51fd\u6570\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5b8c\u6574\u6307\u5357 \u7b80\u4ecb PHP \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u51fd\u6570\u6765\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u6267\u884c\u5e38\u89c1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5305\u62ec\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e\u3002 \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5 \u4f7f\u7528 mysqli_connect() \u51fd\u6570\u5efa\u7acb\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff1a $servername = &#8220;localhost&#8221;; $username = &#8220;root&#8221;; $password = &#8220;password&#8221;; $dbname = &#8220;database_name&#8221;; \/\/ \u521b\u5efa\u4e00\u4e2a MySQL \u8fde\u63a5 $conn = mysqli_connect($servername, $username, $password, $dbname); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48556","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48556"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48556\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}