Quick Tip: Access Oracle LOB directly from PHP

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 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'];

Note: When using oci_fetch_all() lobs are loaded implicitly without having to pass the flag OCI_RETURN_LOBS.

Join the Conversation

2 Comments

Leave a Reply to Simon Cancel reply

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