{"id":593,"date":"2012-11-29T15:37:57","date_gmt":"2012-11-29T14:37:57","guid":{"rendered":"http:\/\/www.speich.net\/articles\/?p=593"},"modified":"2023-10-18T14:05:51","modified_gmt":"2023-10-18T12:05:51","slug":"sql-tip-how-to-bind-null-with-php-and-oracle","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2012\/11\/29\/sql-tip-how-to-bind-null-with-php-and-oracle\/","title":{"rendered":"SQL tip: How to bind NULL with PHP and Oracle"},"content":{"rendered":"\n<p>Neat little trick to bind a null value without using an if clause.<\/p>\n\n\n\n<p>How often have you done something like this when a variable might be null:<\/p>\n\n\n\n<pre><code class=\"language-sql\">$sql = \"SELECT * FROM someTable WHERE col1 = :firstVar\";\nif ($secondVar !== null) {\n   $sql.= \" AND col2 = :secondVar\";\n}\n...\noci_bind_by_name($stmt, ':firstVar', $firstVar);\nif ($secondVar !== null) {\n   oci_bind_by_name($stmt, ':secondVar', $secondVar);\n}<\/code><\/pre>\n\n\n\n<p>Then checkout this, which is quite a bit shorter and a lot easier to read:<\/p>\n\n\n\n<pre><code class=\"language-sql\">$sql = \"SELECT * FROM someTable WHERE col1 = :firstVar\n   AND (col2 = :secondVar OR :secondVar IS NULL)\";\n...\noci_bind_by_name($stmt, ':firstVar', $firstVar);\noci_bind_by_name($stmt, ':secondVar', $secondVar);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Neat little trick to bind a null value without using an if clause. How often have you done something like this when a variable might be null: $sql = &#8220;SELECT * FROM someTable WHERE col1 = :firstVar&#8221;; if ($secondVar !== null) { $sql.= &#8221; AND col2 = :secondVar&#8221;; } &#8230; oci_bind_by_name($stmt, &#8216;:firstVar&#8217;, $firstVar); if ($secondVar &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.speich.net\/articles\/en\/2012\/11\/29\/sql-tip-how-to-bind-null-with-php-and-oracle\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL tip: How to bind NULL with PHP and Oracle&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,281],"tags":[68,69,81],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-php","category-sql","tag-oci","tag-oracle","tag-sql","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":3,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":2360,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/593\/revisions\/2360"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}