{"id":1624,"date":"2021-04-17T11:12:16","date_gmt":"2021-04-17T09:12:16","guid":{"rendered":"https:\/\/www.speich.net\/articles\/?p=1624"},"modified":"2022-10-12T14:43:15","modified_gmt":"2022-10-12T12:43:15","slug":"quick-tip-access-oracle-blob-directly-from-php","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2021\/04\/17\/quick-tip-access-oracle-blob-directly-from-php\/","title":{"rendered":"Quick Tip: Access Oracle LOB directly from PHP"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Access the data from a clob\/blob field without having to call OCI-Lob->load() first<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you fetch (binary) data from an Oracle database field of type BLOB or CLOB with OCI8, the (binary) large object is normally returned as LOB descriptor (an instance of the <a href=\"https:\/\/www.php.net\/manual\/en\/class.OCI-Lob.php\">OCI-Lob class<\/a>). To retrieve the data, you have to call the object&#8217;s load or read method, e.g.:<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>\/\/ load and render an image from an oracle database\n$sql = 'SELECT myBlob FROM myTab WHERE id = :imgId';\n$stmt = oci_parse($dbHandle, $sql);\noci_bind_by_name($stmt, ':imgId', $imgId, 3, OCI_B_INT);\noci_execute($stmt);\n$record = oci_fetch_array($stmt, OCI_ASSOC);\n$photo = $record&#91;'MYBLOB']-&gt;load();\nheader('Content-Type: image\/jpeg');\necho $photo;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The call to the load() method can be omitted by passing the constant OCI_RETURN_LOBS to the fetch method, e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>...\n$record = oci_fetch_array($stmt, OCI_ASSOC + OCI_RETURN_LOBS);\nheader('Content-Type: image\/jpeg');\necho $record&#91;'MYBLOB'];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note: When using <code>oci_fetch_all()<\/code> lobs are loaded implicitly without having to pass the flag <code>OCI_RETURN_LOBS<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Access the data from a clob\/blob field without having to call OCI-Lob->load() first When you fetch (binary) data from an Oracle database field of type BLOB or CLOB with OCI8, the (binary) large object is normally returned as LOB descriptor (an instance of the OCI-Lob class). To retrieve the data, you have to call the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.speich.net\/articles\/en\/2021\/04\/17\/quick-tip-access-oracle-blob-directly-from-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Quick Tip: Access Oracle LOB directly from PHP&#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-1624","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\/1624","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=1624"}],"version-history":[{"count":15,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/1624\/revisions"}],"predecessor-version":[{"id":2168,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/1624\/revisions\/2168"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=1624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=1624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=1624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}