PHP How to manage Oracle LOB data _php Tutorial

Source: Internet
Author: User
We all know that.VARCHAR2 is also a recommended type for Oracle companies. But there is a problem with VARCHAR2: The maximum is only 4,000 characters, which is equivalent to 2000 characters. If the value of a character in your program is greater than 20,002 characters, you cannot satisfy the requirement with VARCHAR2. At this point, you have two choices, one for multiple VARCHAR2, and the other for LOB fields. Here we take a look at the second approach.

Let's take a general look at Oracle's LOB fields. Oracle's LOB types are divided into three kinds: Blob,clob and bfile. Clob called characters Lob,blob and bfile are used to store binary data. The maximum length of clob and BLOBs is 4GB, which stores the values in the Oracle database. Bfile is similar to a blob, but it puts the data in an external file, so it is also called an external blob (External blob).

I think we should not be unfamiliar with MySQL. There are similar data types in MySQL, such as text and blobs. In PHP's MySQL function, the operation of Text/blob is straightforward, just like any other type of data. But in Oracle, things are different. Oracle treats LOBs as a special type of data and cannot be manipulated in a conventional way. For example, you cannot insert a value directly into a LOB field in an INSERT statement, or you can find it with like.

The following is an example of how to use PHP's OCI functions to insert, remove, and query LOB data through several examples of PHP management of Oracle LOB data.

Insert

You cannot insert a value directly into the LOB field with the INSERT statement. In general, as in the following steps:

1. First parse an INSERT statement and return a LOB descriptor

2. Generate a local LOB object with the OCI function

3. Bind the LOB object to the LOB descriptor

4. Execute INSERT statement

5. Assigning values to LOB objects

6. Releasing LOB objects and SQL statement handles

The following example of PHP for managing Oracle LOB data is to store the image file uploaded by the user in a blob (or bfile, with a slightly different operation). The first one is to build a table with the following structure:

 
  
  
  1. CREATE TABLE PICTURES (
  2. ID number,
  3. DESCRIPTION VARCHAR2 (100),
  4. MIME VARCHAR2 (128),
  5. Picture BLOB

If you want to implement an automatic increment of the ID, build another sequence:

CREATE SEQUENCE Pic_seq;

Then there is the PHP program code to process the data.

 
 
  1. Php
  2. Establishing an Oracle database connection
  3. $ Conn = Ocilogon ($user, $password, $SID);
  4. Submit SQL statement to Oracle
  5. Here are two points to note: one is to use the Empty_blob () function. This is the intrinsic function of Oracle,
    Returns a locator for a lob. You can only use this method when inserting lobs into an empty lob set
    character, and then manipulate the locator. The Empty_blob () function is for BLOB type,
    Corresponding to the CLOB is Empty_clob (). The second is the part behind the returning, putting the picture
    Returns, allowing the OCI function of PHP to be processed.
  6. $ stmt = Ociparse ($conn, "INSERT into PICTURES (ID, description, picture)
  7. VALUES (Pic_seq. Nextval, ' $description ', ' $lob _upload_type '
    , Empty_blob ()) returning picture into Icture ");
  8. Generates a descriptor for a local LOB object. Note the second parameter of the function: Oci_d_lob,
    Represents the generation of a lob object. Other possibilities are oci_d_file and Oci_d_rowid,
    Corresponds to bfile and rowID objects, respectively.
  9. $ LOB = Ocinewdescriptor ($conn, Oci_d_lob);
  10. Binds the resulting LOB object to the locator returned by the preceding SQL statement.
  11. Ocibindbyname ($stmt, ':P icture ', & $lob,-1, Oci_b_blob);
  12. Ociexecute ($stmt);
  13. The data is stored in the LOB object. Because the source data here is a file, the LOB is used directly
    The SaveFile () method of the object. Other methods of LOB objects are: Save () and load (),
    Used to save and retrieve data separately. But there is only one way to bfile type is save ()
  14. if ($lob->savefile ($lob _upload)) {
  15. Ocicommit ($conn);
  16. echo "Upload success" br> ";
  17. }else{
  18. echo "Upload failed" br> ";
  19. }
  20. Releasing LOB objects
  21. Ocifreedesc ($LOB);
  22. Ocifreestatement ($stmt);
  23. Ocilogoff ($conn);
  24. ? >

There is one more point to note: The value of the LOB field is at least 1 characters, so before save () or SaveFile (), make sure the value cannot be empty. Otherwise, Oracle will make an error.

Remove

PHP manages Oracle LOB data to fetch data from one lob in two ways. One is to generate a LOB object, bind to a locator returned by a SELECT statement, and then fetch the data with the LOB object's load () method, and the ocifetch*** function directly with PHP. The first method is much more troublesome than the second one, so I'll just talk about the second approach.

Or use the table above.

 
 
  1. Php
  2. $ Conn = Ocilogon ($user, $password, $SID);
  3. $ stmt = Ociparse ($conn, "SELECT *
    From PICTURES WHERE ID= $pictureid ");
  4. Ociexecute ($stmt);
  5. The secret is on the third parameter of Pcifetchinfo:
    Oci_return_lobs. The third parameter is the fetch pattern,
    If oci_return_lobs, put the LOB value directly into the knot
    Instead of the LOB locator, the load () method of the LOB object is not used.
  6. if (Ocifetchinto ($stmt, $result, Oci_assoc+oci_return_lobs))
  7. {
  8. echo "Content-type:". Stripslashes ($result [MIME]);
  9. Echo stripslashes ($result [picture]);
  10. }
  11. Ocifreestatement ($stmt);
  12. Ocilogoff ($conn);
  13. ? >

This program is used to display data (images) that are placed in the LOB. Call the method (assuming the script name is getpicture.php):

 
  
  
  1. SRC = "getpicture.php?" Pictureid = About
    Alt

Inquire

As already mentioned, PHP managed Oracle LOB data cannot be matched with like in LOB fields for Oracle. What do we do? It's not complicated, Oracle has an anonymous package called Dbms_lob, which has all the necessary procedures for manipulating lobs.

Suppose there is a table like this:

 
  
  
  1. CREATE TABLE Articles (
  2. ID number,
  3. TITLE VARCHAR2 (100),
  4. CONTENT CLOB

The contents of the article are placed in the Content field.

Now we want to find out that the content contains the "PHP Chinese user" article, you can do this:

 
 
  1. Php
  2. $ Conn = Ocilogon ($user, $password, $SID);
  3. Dbms_lob is used in the WHERE clause. InStr process. It has four parameters,
    The first two locators (which can be represented directly in the field), respectively, representing the LOB
    And the string to find, followed by two to indicate the starting offset and the number of occurrences.
    Note that it must be judged by its return value, which is greater than 0.
  4. $ stmt = Ociparse ($conn, "select * from articles
    WHERE Dbms_lob. INSTR (CONTENT, ' php Chinese user ', 1, 1) > 0″);
  5. Ociexecute ($stmt);
  6. if (Ocifetchinto ($stmt, $result, Oci_assoc+oci_return_lobs))
  7. {
  8. ...
  9. }
  10. Ocifreestatement ($stmt);
  11. Ocilogoff ($conn);
  12. ? >

Oracle also provides a number of procedures for manipulating LOB data, such as length, substr, and so on. For their detailed usage, you can consider Oracle's development manuals.

There are so many things to say about PHP's operations for managing Oracle LOB data. Since I have not been in touch with Oracle for a long time, there may be errors in this article, and you are welcome to criticize.


http://www.bkjia.com/PHPjc/446262.html www.bkjia.com true http://www.bkjia.com/PHPjc/446262.html techarticle we all know that VARCHAR2 is also a recommended type for Oracle companies. But there is a problem with VARCHAR2: The maximum is only 4,000 characters, which is equivalent to 2000 characters. If your program ...

  • Related Article

    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.