{"id":708,"date":"2020-03-04T11:11:21","date_gmt":"2020-03-04T03:11:21","guid":{"rendered":"http:\/\/ihongchao.com\/?p=708"},"modified":"2020-03-04T11:11:21","modified_gmt":"2020-03-04T03:11:21","slug":"php-mysqli_stmt-bind_param%e6%8a%a5%e9%94%99%e7%9a%84%e5%8e%9f%e5%9b%a0%e4%b8%8e%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/ihongchao.com\/post\/708\/","title":{"rendered":"PHP Mysqli_stmt->bind_param\u62a5\u9519\u7684\u539f\u56e0\u4e0e\u89e3\u51b3\u529e\u6cd5"},"content":{"rendered":"

\u6700\u8fd1\u7531\u4e8e\u5347\u7ea7PHP\u7248\u672c\u5bfc\u81f4wordpress\u7684\u4e00\u4e2a\u63d2\u4ef6\u8fd0\u884c\u65f6\u62a5\u9519\uff0c\u4e5f\u6ca1\u6709\u5176\u4ed6\u63d2\u4ef6\u53ef\u4f9b\u66ff\u4ee3\u3002\u4fd7\u8bdd\u8bf4\uff0c\u81ea\u5df1\u52a8\u624b\uff0c\u4e30\u8863\u8db3\u98df\u3002\u4e8e\u662f\u6211\u5f00\u59cb\u81ea\u5df1\u7f16\u5199\u63d2\u4ef6\u3002\u63d2\u4ef6\u9700\u8981\u7528\u5230\u6570\u636e\u5e93\u4e2d\u7684\u5185\u5bb9\uff0c\u4e8e\u662f\u6211\u7f16\u5199\u67e5\u8be2\u67e5\u8be2\u8bed\u53e5\uff0c\u4f7f\u7528\u4e86\u636e\u8bf4\u8f83\u4e3a\u5b89\u5168\u7684mysqli::prepare\u65b9\u6cd5\u3002\u4f46\u662f\u6709\u4e00\u4e2a\u62a5\u9519\u56f0\u6270\u4e86\u6211\u5f88\u4e45\u3002<\/p>\n

\u62a5\u9519\u5185\u5bb9\u4e3a\u201cFatal error: Uncaught Error: Call to a member function bind_param() on boolean in<\/strong>\u201d\uff0c\u4e2d\u6587\u76f4\u8bd1\u4e3a\u201c\u81f4\u547d\u9519\u8bef\uff1a\u672a\u6355\u83b7\u9519\u8bef\uff1a\u8c03\u7528\u4e2d\u5e03\u5c14\u503c\u4e0a\u7684\u6210\u5458\u51fd\u6570bind_param\uff08\uff09<\/strong>\u201d\uff0c\u95ee\u9898\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

$conn = new mysqli(\"db_host\", \"db_user\", \"db_pw\", \"db_name\");\r\n$stmt1 = $conn->prepare(\"SELECT col FROM tablename1 WHERE id=?\");\r\n$stmt1->bind_param(\"i\", 1);\r\n$stmt1->execute();\r\n$stmt1->bind_result($col1);\r\nwhile($stmt1->fetch()){\r\n    $stmt2 = $conn->prepare(\"SELECT col FROM tablename2 WHERE id=?\");\r\n    $stmt2->bind_param(\"i\", $col1);\r\n    $stmt2->execute();\r\n    $stmt2->bind_result(col2);\r\n    ......\r\n}\r\n$stmt2->close();\r\n$stmt1->close();\r\n$conn->close();<\/pre>\n

\u95ee\u9898\u53d1\u751f\u5728\u4e0a\u8ff0\u4ee3\u7801\u76848\u884c\uff0c\u4e5f\u5c31\u662fbind_param\u8bed\u53e5\u3002\u9605\u8bfb\u62a5\u9519\u5f97\u77e5\uff0c\u9519\u8bef\u539f\u56e0\u672a\u6355\u83b7\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u867d\u7136\u7a0b\u5e8f\u5728\u6267\u884c\u5230\u7b2c8\u884c\u65f6\u53d1\u73b0\u4e86\u95ee\u9898\uff0c\u4f46\u662f\u95ee\u9898\u4e0d\u4e00\u5b9a\u51fa\u5728\u7b2c8\u884c\u3002\u6709\u53ef\u80fd\u662f\u56e0\u4e3a\u524d\u8fb9\u67d0\u4e9b\u5730\u65b9\u7684\u53c2\u6570\u6709\u672a\u88ab\u53d1\u73b0\u7684\u9519\u8bef\uff0c\u4ee3\u7801\u6267\u884c\u5230\u7b2c8\u884c\u65f6\u624d\u53d1\u73b0\u4e86\u9519\u8bef\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u9519\u8bef\u7a76\u7adf\u53d1\u751f\u5728\u54ea\u91cc\u3002\u800c\u4e14\uff0c\u524d\u8fb9\u4e5f\u6709\u4e00\u4e2abind_param\uff0c\u5e76\u6ca1\u6709\u62a5\u9519\u3002<\/p>\n

\u5f80\u524d\u770b\uff0c\u4f3c\u4e4e\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u731c\u6d4b\uff0c\u4e00\u4e2aPHP\u6587\u4ef6\u4e2d\u53ea\u5141\u8bb8\u540c\u65f6\u5b9a\u4e49\u4e00\u4e2amysqli_stmt\u5bf9\u8c61\u5417\uff1f\u4e8e\u662f\u6211\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u5173\u95ed\u7b2c\u4e00\u4e2amysqli_stmt\u5bf9\u8c61\u540e\u518d\u5f00\u542f\u7b2c\u4e8c\u4e2amysqli_stmt\u5bf9\u8c61\uff1a<\/p>\n

$conn = new mysqli(\"db_host\", \"db_user\", \"db_pw\", \"db_name\"); \r\n$stmt1 = $conn->prepare(\"SELECT col FROM tablename1 WHERE id=?\"); \r\n$stmt1->bind_param(\"i\", 1); \r\n$stmt1->execute(); \r\n$stmt1->bind_result($col1);\r\nif($stmt1->fetch()){\r\n    $param = $col1;\r\n}else{\r\n    $param = false;\r\n}\r\n$stmt1->close();\r\nif($param !== false){\r\n    $stmt2 = $conn->prepare(\"SELECT col FROM tablename2 WHERE id=?\");\r\n    $stmt2->bind_param(\"i\", $param);\r\n    $stmt2->execute();\r\n    $stmt2->bind_result(col2);\r\n    ......\r\n    $stmt2->close();\r\n}\r\n$conn->close();<\/pre>\n

\u4ee5\u4e0a\u6539\u5199\u5c06\u5148\u524d\u7684\u540c\u65f6\u5b58\u5728\u7684stmt1\u548cstmt2\u6539\u5199\u4e3a\u4e86\u5148\u7ed3\u675f\u4e00\u4e2a\u518d\u8fd0\u884c\u4e00\u4e2a\u3002<\/p>\n

\u8fd0\u884c\u6d4b\u8bd5\uff0c\u6210\u529f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1\u7531\u4e8e\u5347\u7ea7PHP\u7248\u672c\u5bfc\u81f4wordpress\u7684\u4e00\u4e2a\u63d2\u4ef6\u8fd0\u884c\u65f6\u62a5\u9519\uff0c\u4e5f\u6ca1\u6709\u5176\u4ed6\u63d2\u4ef6\u53ef\u4f9b\u66ff\u4ee3\u3002\u4fd7\u8bdd\u8bf4\uff0c\u81ea\u5df1\u52a8\u624b\uff0c\u4e30 … \u7ee7\u7eed\u9605\u8bfbPHP Mysqli_stmt->bind_param\u62a5\u9519\u7684\u539f\u56e0\u4e0e\u89e3\u51b3\u529e\u6cd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10,120],"_links":{"self":[{"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/posts\/708"}],"collection":[{"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/comments?post=708"}],"version-history":[{"count":5,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"predecessor-version":[{"id":713,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/posts\/708\/revisions\/713"}],"wp:attachment":[{"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihongchao.com\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}