{"id":47976,"date":"2024-12-02T10:14:47","date_gmt":"2024-12-02T02:14:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47976\/"},"modified":"2024-12-02T10:14:47","modified_gmt":"2024-12-02T02:14:47","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e%e6%95%b0%e6%8d%ae%e5%ba%93%e4%ba%a4%e4%ba%92%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47976\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>PHP \u901a\u8fc7\u51fd\u6570\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5305\u62ec\uff1a\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u8c03\u7528 mysqli \u51fd\u6570\u3002\u67e5\u8be2\u6570\u636e\u5e93\uff1a\u901a\u8fc7 query() \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2\u3002\u63d2\u5165\u6570\u636e\uff1a\u4f7f\u7528 prepare() \u548c bind_param() \u51c6\u5907\u548c\u7ed1\u5b9a\u8bed\u53e5\uff0c\u7136\u540e\u6267\u884c execute()\u3002\u66f4\u65b0\u6570\u636e\uff1a\u540c\u4e0a\uff0c\u4f46\u4f7f\u7528 UPDATE \u8bed\u53e5\u3002\u5220\u9664\u6570\u636e\uff1a\u540c\u4e0a\uff0c\u4f46\u4f7f\u7528 DELETE \u8bed\u53e5\u3002\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u8c03\u7528 close() \u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729947150671ce60e18a23.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92<\/h2>\n<p>PHP \u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\uff0c\u7528\u4e8e\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002\u672c\u6559\u7a0b\u5c06\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u51fd\u6570\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u6765\u5e2e\u52a9\u60a8\u7406\u89e3\u5176\u7528\u6cd5\u3002<\/p>\n<p><strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<pre>$servername = \"localhost\";\n$username = \"user\";\n$password = \"password\";\n$dbname = \"database\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n  die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}<\/pre>\n<p><strong>\u67e5\u8be2\u6570\u636e\u5e93<\/strong><\/p>\n<pre>\/\/ \u6267\u884c\u67e5\u8be2\n$sql = \"SELECT * FROM customers\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u904d\u5386\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n  while($row = $result-&gt;fetch_assoc()) {\n    echo \"ID: \" . $row[\"id\"] . \"&lt;br&gt;\";\n    echo \"\u59d3\u540d: \" . $row[\"name\"] . \"&lt;br&gt;\";\n    echo \"\u7535\u5b50\u90ae\u4ef6: \" . $row[\"email\"] . \"&lt;br&gt;&lt;br&gt;\";\n  }\n} else {\n  echo \"0 \u7ed3\u679c\";\n}<\/pre>\n<p><strong>\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<pre>\/\/ \u8981\u63d2\u5165\u7684\u6570\u636e\n$name = \"John Doe\";\n$email = \"johndoe@example.com\";\n\n\/\/ \u51c6\u5907\u548c\u7ed1\u5b9a\u8bed\u53e5\n$stmt = $conn-&gt;prepare(\"INSERT INTO customers (name, email) VALUES (?, ?)\");\n$stmt-&gt;bind_param(\"ss\", $name, $email);\n\n\/\/ \u6267\u884c\u8bed\u53e5\n$stmt-&gt;execute();\n\n\/\/ \u68c0\u67e5\u63d2\u5165\u662f\u5426\u6210\u529f\nif ($stmt-&gt;affected_rows &gt; 0) {\n  echo \"\u8bb0\u5f55\u63d2\u5165\u6210\u529f\";\n} else {\n  echo \"\u8bb0\u5f55\u63d2\u5165\u5931\u8d25\";\n}\n\n\/\/ \u5173\u95ed\u8bed\u53e5\n$stmt-&gt;close();<\/pre>\n<p><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/p>\n<pre>\/\/ \u8981\u66f4\u65b0\u7684\u6570\u636e\n$id = 1;\n$new_name = \"Jane Doe\";\n\n\/\/ \u51c6\u5907\u548c\u7ed1\u5b9a\u8bed\u53e5\n$stmt = $conn-&gt;prepare(\"UPDATE customers SET name = ? WHERE id = ?\");\n$stmt-&gt;bind_param(\"si\", $new_name, $id);\n\n\/\/ \u6267\u884c\u8bed\u53e5\n$stmt-&gt;execute();\n\n\/\/ \u68c0\u67e5\u66f4\u65b0\u662f\u5426\u6210\u529f\nif ($stmt-&gt;affected_rows &gt; 0) {\n  echo \"\u8bb0\u5f55\u66f4\u65b0\u6210\u529f\";\n} else {\n  echo \"\u8bb0\u5f55\u66f4\u65b0\u5931\u8d25\";\n}\n\n\/\/ \u5173\u95ed\u8bed\u53e5\n$stmt-&gt;close();<\/pre>\n<p><strong>\u5220\u9664\u6570\u636e<\/strong><\/p>\n<pre>\/\/ \u8981\u5220\u9664\u7684 ID\n$id = 1;\n\n\/\/ \u51c6\u5907\u548c\u7ed1\u5b9a\u8bed\u53e5\n$stmt = $conn-&gt;prepare(\"DELETE FROM customers WHERE id = ?\");\n$stmt-&gt;bind_param(\"i\", $id);\n\n\/\/ \u6267\u884c\u8bed\u53e5\n$stmt-&gt;execute();\n\n\/\/ \u68c0\u67e5\u5220\u9664\u662f\u5426\u6210\u529f\nif ($stmt-&gt;affected_rows &gt; 0) {\n  echo \"\u8bb0\u5f55\u5220\u9664\u6210\u529f\";\n} else {\n  echo \"\u8bb0\u5f55\u5220\u9664\u5931\u8d25\";\n}\n\n\/\/ \u5173\u95ed\u8bed\u53e5\n$stmt-&gt;close();<\/pre>\n<p><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<pre>\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ePHP \u51fd\u6570,\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff1f\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 PHP \u901a\u8fc7\u51fd\u6570\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5305\u62ec\uff1a\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u8c03\u7528 mysqli \u51fd\u6570\u3002\u67e5\u8be2\u6570\u636e\u5e93\uff1a\u901a\u8fc7 query() \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2\u3002\u63d2\u5165\u6570\u636e\uff1a\u4f7f\u7528 prepare() \u548c bind_param() \u51c6\u5907\u548c\u7ed1\u5b9a\u8bed\u53e5\uff0c\u7136\u540e\u6267\u884c execute()\u3002\u66f4\u65b0\u6570\u636e\uff1a\u540c\u4e0a\uff0c\u4f46\u4f7f\u7528 UPDATE \u8bed\u53e5\u3002\u5220\u9664\u6570\u636e\uff1a\u540c\u4e0a\uff0c\u4f46\u4f7f\u7528 DELETE \u8bed\u53e5\u3002\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u8c03\u7528 close() \u65b9\u6cd5\u3002 PHP \u51fd\u6570\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92 PHP \u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\uff0c\u7528\u4e8e\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002\u672c\u6559\u7a0b\u5c06\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u51fd\u6570\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u6765\u5e2e\u52a9\u60a8\u7406\u89e3\u5176\u7528\u6cd5\u3002 \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5 $servername = &#8220;localhost&#8221;; $username = &#8220;user&#8221;; $password = &#8220;password&#8221;; $dbname = &#8220;database&#8221;; \/\/ \u521b\u5efa\u8fde\u63a5 $conn = new mysqli($servername, $username, $password, $dbname); \/\/ \u68c0\u67e5\u8fde\u63a5 if ($conn-&gt;connect_error) { die(&#8220;\u8fde\u63a5\u5931\u8d25: [&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-47976","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47976","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=47976"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47976\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}