{"id":8084,"date":"2021-07-23T13:28:06","date_gmt":"2021-07-23T18:28:06","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=8084"},"modified":"2021-07-23T13:28:06","modified_gmt":"2021-07-23T18:28:06","slug":"php-oci_bind_by_name-caveat-a-k-a-a-reminder-to-read-the-documentation","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=8084","title":{"rendered":"PHP oci_bind_by_name Caveat (a.k.a. a reminder to read the documentation)"},"content":{"rendered":"<p>This is readily apparent when you actually read <a href=\"https:\/\/www.php.net\/manual\/en\/function.oci-bind-by-name.php\" target=\"_blank\" rel=\"noopener\">the documentation for oci_bind_by_name<\/a> \u2026 but, if you quickly skim through the doc and get mostly what it\u2019s telling you, you can lose data when implementing oci_bind_by_name in your code. That\u2019s because the variable that gets bound into the name is a <em>reference<\/em>. It\u2019s right there in the description<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"289\" class=\"wp-image-8085\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image.png 354w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-300x245.png 300w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><\/p>\n<p>And again in the more verbose part of the description<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1055\" height=\"107\" class=\"wp-image-8086\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1.png 1055w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1-300x30.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1-1024x104.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1-768x78.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/07\/word-image-1-750x76.png 750w\" sizes=\"auto, (max-width: 1055px) 100vw, 1055px\" \/><\/p>\n<p>Well? They really mean It!<\/p>\n<p>I usually iterate through arrays with for $x=0; $x&lt;count($arrayOfData);$x++ &#8212; which is fine because $arrayOfData[$x] is a valid location in memory that contains the data I want. I had a program, though, that accepted user input. Input that frequently contains a bunch of extraneous newline characters in the middle of the data. To avoid making the users clean up their input, I just threw an array_filter on the input. But I don\u2019t <em>need<\/em> the reorganized data, so I didn\u2019t \u201cwaste\u201d the cycles to re-index the filtered array. Instead, I used<\/p>\n<pre>$iIterator=0;\r\nfor( $itemInput in array_filter( $arrayOfData ) ) {\r\n     oci_bind_by_name($stmt, \u2018:placeholder\u2019.$iIterator++, $itemInput);\r\n}<\/pre>\n<p>Which is fine in most circumstances. But, with oci_bind_by_name \u2026 the memory address where $inputItem happened to be stashed is bound to the names :placeholder0, :placeholder1, \u2026, :placeholder<em>n<\/em> \u2013 that memory address <em>happened<\/em> to still contain the last element of arrayOfData I happened to stash there because the server isn\u2019t so busy that memory is being reallocated a fraction of a second later. But this loop does <em>not<\/em> bind each element of arrayOfData to its corresponding placeholder &#8212; instead of saying &#8220;select * from MyTable where ID IN (1,2,3,4,5)&#8221; &#8230; I had &#8220;select * from MyTable where ID IN (5,5,5,5,5)&#8221;.<\/p>\n<p>In this case you <em>need<\/em> to use the array index<\/p>\n<pre>for( $x = 0; $x &lt; count( $arrayOfData ); $x++ ) {\r\n     $strPlaceholderName\u00a0=\u00a0\":inputclli\".$x;\r\n     oci_bind_by_name($stmt, $strPlaceholderName, $arrayOfData[$x]);\r\n}<\/pre>\n<p>So each unique memory address is bound to a placeholder.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is readily apparent when you actually read the documentation for oci_bind_by_name \u2026 but, if you quickly skim through the doc and get mostly what it\u2019s telling you, you can lose data when implementing oci_bind_by_name in your code. That\u2019s because the variable that gets bound into the name is a reference. It\u2019s right there in &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,1004],"tags":[1431,35,1432],"class_list":["post-8084","post","type-post","status-publish","format-standard","hentry","category-coding","category-oracle","tag-oci_bind_by_name","tag-php","tag-rtfm"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8084"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8084\/revisions"}],"predecessor-version":[{"id":8087,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8084\/revisions\/8087"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}