In PHP, LOBs are manipulated using a descriptor. To show this, in SQL*Plus create a table
that has a BLOB column:
SQL> create table mybtab (blobid number, blobdata blob);
PHP code to insert data into this table is:
navioo_blobinsert.php
<?php
$c = oci_connect('hr', 'hrpwd', '//localhost/XE');
$myblobid = 123;
$myv = 'a very large amount of binary data';
$lob = oci_new_descriptor($c, OCI_B_LOB);
$s = oci_parse($c,
'INSERT INTO mybtab (blobid, blobdata) '
. 'VALUES(:myblobid, EMPTY_BLOB()) '
. 'RETURNING blobdata INTO :blobdata');
oci_bind_by_name($s, ':MYBLOBID', $myblobid);
oci_bind_by_name($s, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
oci_execute($s, OCI_DEFAULT);
$lob->save($myv);
oci_commit($c);
?>
The RETURNING clause returns the Oracle LOB locator of the new row. By binding as
OCI_B_BLOB, the PHP descriptor in $lob references this locator. The $lob->save() method
then stores the data in $myv into the BLOB column. The OCI_DEFAULT flag is used for
oci_execute() so the descriptor remains valid for the save method. The commit concludes
the insert and makes the data visible to other database users
If the application uploads LOB data using a web form, it can be inserted directly from the
upload directory with $lob->import($filename). PHP’s maximum allowed size for
uploaded files is set in php.ini using the upload_max_filesize parameter.
When fetching a LOB, OCI8 returns the LOB descriptor, and the data is retrieved by
using a load() or read() method:
<?
//---------
$query = 'SELECT blobdata FROM mybtab WHERE blobid = 123';
$stmt = oci_parse ($c, $query);
oci_execute($stmt);
$arr = oci_fetch_assoc($stmt);
$data = $arr['BLOBDATA']->load();
//-----
?>
The PHP manual recommends using read() instead of load() because it allows the data
size of data to be limited. This can help avoid the PHP process abruptly terminating when it
reaches its allocated maximum memory size, set with memory_limit in php.ini.
Using CLOBs is almost identical to using BLOBs. The bind type becomes
OCI_B_CLOB, and the table must obviously contain a CLOB column.
OCI-Lob
|