{"id":2468,"date":"2016-05-13T11:24:26","date_gmt":"2016-05-13T18:24:26","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2468"},"modified":"2016-05-17T14:04:42","modified_gmt":"2016-05-17T21:04:42","slug":"a-note-on-pdo-prepared-statements","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2468","title":{"rendered":"A note on PDO prepared statements"},"content":{"rendered":"<p>It wasn\u2019t obvious to me that you need to have a different bindParameter statement each time  you use a variable in the query. This won\u2019t work.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ query for title information\n$qry&nbsp;&nbsp;= &quot;SELECT * &quot;;\n$qry .= &quot;FROM `website_database`.`product`, `website_database`.`product_instance` &quot;;\n$qry .= &quot;WHERE product.id = :productID &quot;;\n$qry .= &quot;AND product_instance.product_id = :productID&quot;;\n\n$stmt = $dbWG-&gt;prepare($qry);\n$stmt-&gt;bindParam(&#039;:productID&#039;, $productID);\n$stmt-&gt;execute();<\/code><\/pre><\/p>\n<p>MySql doesn\u2019t look for every instance of :productID in the query and substitute $productID. This is how you do it:<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ query for title information\n$qry&nbsp;&nbsp;= &quot;SELECT * &quot;;\n$qry .= &quot;FROM `website_database`.`product`, `website_database`.`product_instance` &quot;;\n$qry .= &quot;WHERE product.id = :productID1 &quot;;\n$qry .= &quot;AND product_instance.product_id = :productID2&quot;;\n\n$stmt = $dbWG-&gt;prepare($qry);\n$stmt-&gt;bindParam(&#039;:productID1&#039;, $productID);\n$stmt-&gt;bindParam(&#039;:productID2&#039;, $productID);\n$stmt-&gt;execute();<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It wasn\u2019t obvious to me that you need to have a different bindParameter statement each time you use a variable in the query. This won\u2019t work. \/\/ query for title information $qry&nbsp;&nbsp;= &quot;SELECT * &quot;; $qry .= &quot;FROM `website_database`.`product`, `website_database`.`product_instance` &quot;; $qry .= &quot;WHERE product.id = :productID &quot;; $qry .= &quot;AND product_instance.product_id = :productID&quot;; $stmt &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2468\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A note on PDO prepared statements<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-2468","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2468"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}