{"id":47353,"date":"2024-12-02T17:02:50","date_gmt":"2024-12-02T09:02:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47353\/"},"modified":"2024-12-02T17:02:50","modified_gmt":"2024-12-02T09:02:50","slug":"php-%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e7%bb%91%e5%ae%9a%e4%b8%8e%e5%85%b6%e4%bb%96%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e7%b1%bb%e4%bc%bc%e7%89%b9%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47353\/","title":{"rendered":"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f<\/h1>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729912305671c5df11d411.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a<\/strong><\/p>\n<p>\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4e3a\u51fd\u6570\u53c2\u6570\u6307\u5b9a\u503c\u7684\u673a\u5236\u3002\u5b83\u63d0\u4f9b\u4e86\u6bd4\u76f4\u63a5\u4f20\u9012\u503c\u66f4\u7075\u6d3b\u548c\u5b89\u5168\u7684\u65b9\u6cd5\u3002<\/p>\n<p>PHP \u4e2d\u7684\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7c7b\u4f3c\u7279\u6027\u7c7b\u4f3c\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>Java\uff1a<\/strong> PreparedStatement<\/li>\n<li><strong>C#\uff1a<\/strong> SqlCommand.Parameters<\/li>\n<li><strong>Python\uff1a<\/strong> cursor.executemany<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bindParam<\/code> \u6216 <code>bindValue<\/code> \u51fd\u6570\u6765\u7ed1\u5b9a\u53c2\u6570\u3002<\/p>\n<pre>bindParam(string $parameter, mixed $variable, int $data_type, int $length = null, int $driver_options = null)\nbindValue(string $parameter, mixed $variable, int $data_type, int $length = null, int $driver_options = null)<\/pre>\n<ul>\n<li><code>$parameter<\/code>\uff1a\u53c2\u6570\u540d\u79f0\uff0c\u4ee5\u5192\u53f7\u5f00\u5934\uff08\u4f8b\u5982\uff1a&#8221;:name&#8221;\uff09<\/li>\n<li><code>$variable<\/code>\uff1a\u8981\u7ed1\u5b9a\u7684\u53d8\u91cf<\/li>\n<li><code>$data_type<\/code>\uff1a\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\uff1a<code>PDO::PARAM_STR<\/code>\u3001<code>PDO::PARAM_INT<\/code>\uff09<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<pre>$stmt = $pdo-&gt;prepare(\"SELECT * FROM users WHERE name = :name\");\n\n\/\/ \u7ed1\u5b9a\u53c2\u6570\n$stmt-&gt;bindParam(':name', $name, PDO::PARAM_STR);\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$stmt-&gt;execute();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u7684\u771f\u5b9e\u6848\u4f8b\uff1a<\/p>\n<pre>\/\/ \u8fde\u63a5\u6570\u636e\u5e93\n$pdo = new PDO(...);\n\n\/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\n$name = $_GET['name'];\n\n\/\/ \u51c6\u5907\u67e5\u8be2\n$stmt = $pdo-&gt;prepare(\"SELECT * FROM users WHERE name = :name\");\n\n\/\/ \u7ed1\u5b9a\u53c2\u6570\n$stmt-&gt;bindParam(':name', $name, PDO::PARAM_STR);\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$stmt-&gt;execute();\n\n\/\/ \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\n$users = $stmt-&gt;fetchAll();<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\uff0c\u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u56e0\u4e3a SQL \u8bed\u53e5\u662f\u9884\u7f16\u8bd1\u7684\uff0c\u5e76\u4e14\u53ea\u5728\u6267\u884c\u65f6\u624d\u4f20\u9012\u53c2\u6570\u503c\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7c7b\u4f3c\u7279\u6027\uff1f\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4e3a\u51fd\u6570\u53c2\u6570\u6307\u5b9a\u503c\u7684\u673a\u5236\u3002\u5b83\u63d0\u4f9b\u4e86\u6bd4\u76f4\u63a5\u4f20\u9012\u503c\u66f4\u7075\u6d3b\u548c\u5b89\u5168\u7684\u65b9\u6cd5\u3002 PHP \u4e2d\u7684\u53c2\u6570\u7ed1\u5b9a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7c7b\u4f3c\u7279\u6027\u7c7b\u4f3c\uff0c\u4f8b\u5982\uff1a Java\uff1a PreparedStatement C#\uff1a SqlCommand.Parameters Python\uff1a cursor.executemany \u8bed\u6cd5 \u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528bindParam \u6216 bindValue \u51fd\u6570\u6765\u7ed1\u5b9a\u53c2\u6570\u3002 bindParam(string $parameter, mixed $variable, int $data_type, int $length = null, int $driver_options = null) bindValue(string $parameter, mixed $variable, int $data_type, int $length = null, int $driver_options = null) $parameter\uff1a\u53c2\u6570\u540d\u79f0\uff0c\u4ee5\u5192\u53f7\u5f00\u5934\uff08\u4f8b\u5982\uff1a&#8221;:name&#8221;\uff09 $variable\uff1a\u8981\u7ed1\u5b9a\u7684\u53d8\u91cf $data_type\uff1a\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\uff1aPDO::PARAM_STR\u3001PDO::PARAM_INT\uff09 \u7528\u6cd5 $stmt [&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-47353","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47353","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=47353"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47353\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}