Php--pdo large Object (lobs)

Source: Internet
Author: User
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 ();? >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.