At some point in time, the application may need to store "large" data in the database. "Big" usually means "about 4kb or more", although some databases can easily process up to 32kb of data before the data reaches "large". Large objects can be either text or binary in nature. Using PDO in Pdostatement::bindparam () or Pdostatement::bindcolumn () calls::P Aram_lob type Code allows PDO to use large data types. PDO::P Aram_lob tells PDO to map the data as a stream so that it can be manipulated using the PHP Streams API.
Example #1 display a picture from the database
The following example binds a LOB to a $lob variable and then sends it to the browser using Fpassthru (). Because lobs represent a stream, functions like fgets (), Fread (), and stream_get_contents () can be used on top of it.
<?php $db = new PDO (' odbc:sample ', ' db2inst1 ', ' ibmdb2 '); $stmt = $db->prepare ("Select ContentType, imagedata from images where id=?"); $stmt->execute (Array ($_get[' id ')); $stmt->bindcolumn (1, $type, PDO::P aram_str, N); $stmt->bindcolumn (2, $lob, PDO::P aram_lob); $stmt->fetch (pdo::fetch_bound); Header ("Content-type: $type"); Fpassthru ($LOB);? >
Example #2 inserting a picture into the database
The following example opens a file and passes the file handle to PDO as a LOB insert. PDO allows the database to get the contents of the file in the most efficient way possible.
<?php $db = new PDO (' odbc:sample ', ' db2inst1 ', ' ibmdb2 '); $stmt = $db->prepare ("INSERT into images (ID, contenttype, ImageData) VALUES (?,?,?)"); $id = get_new_id (); Call a function to assign a new ID //Suppose to process a file upload //can find more information in the PHP document $fp = fopen ($_files[' file ' [' Tmp_name '], ' RB '); $stmt->bindparam (1, $id); $stmt->bindparam (2, $_files[' file ' [' type ']); $stmt->bindparam (3, $FP, PDO::P aram_lob); $db->begintransaction (); $stmt->execute (); $db->commit ();? >
Example #3 inserting a picture into the database: Oracle
For inserting a lob,oracle from a file is slightly different. You must insert after the transaction, or the newly inserted LOB will be implicitly committed at 0 length when the query is executed:
<?php $db = new PDO (' OCI: ', ' Scott ', ' Tiger '); $stmt = $db->prepare ("INSERT into images (ID, contenttype, ImageData)". VALUES (?,?, Empty_blob ()) Returning imagedata into? "); $id = get_new_id (); Call a function to assign a new ID //Suppose to process a file upload //can find more information in the PHP document $fp = fopen ($_files[' file ' [' Tmp_name '], ' RB '); $stmt->bindparam (1, $id); $stmt->bindparam (2, $_files[' file ' [' type ']); $stmt->bindparam (3, $FP, PDO::P aram_lob); $stmt->begintransaction (); $stmt->execute (); $stmt->commit ();? >