Quick Tip: Access Oracle BLOB directly from PHP

Access the data from a blob/clob 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 object’s load or read method, e.g.:

// load and render an image from an oracle database
$sql = 'SELECT myBlob FROM myTab WHERE id = :imgId';
$stmt = oci_parse($dbHandle, $sql);
oci_bind_by_name($stmt, ':imgId', $imgId, 3, OCI_B_INT);
oci_execute($stmt);
$record = oci_fetch_array($stmt, OCI_ASSOC);
$photo = $record['MYBLOB']->load();
header('Content-Type: image/jpeg');
echo $photo;

The call to the load() method can be omitted by passing the constant OCI_RETURN_LOBS to the fetch method, e.g.

...
$record = oci_fetch_array($stmt, OCI_ASSOC + OCI_RETURN_LOBS);
header('Content-Type: image/jpeg');
echo $record['MYBLOB'];

Leave a comment

Your email address will not be published. Required fields are marked *