Accessing Oracle lob using php in the bsstructure

Source: Internet
Author: User
Tags html header php database

PHP ("PHP: Hypertext Preprocessor") is a widely used open source (Open Source)Source code) And can be embedded into the HTML multi-purpose scripting language. Its syntax is similar to C, Java, and Perl and is easy to learn. The main goal of this language is to allow Web developers to quickly write dynamically generated web pages. However, PHP functions are not limited to this. PHP is generally considered to be able to implement complex programming tasks faster and more effectively, it is precisely because it is more stable and consumes less resources that it has become a necessary web script design language for the development of B/S structure systems, playing a role similar to middleware, that is, Syntax Parsing and execution.

Oracle lob Data Model

In B/S (Browser/Server, Browser/Server) application systems, more than just simple text information needs to be stored, it also includes images, audio and video materials, or ultra-long texts. For example, if a document system is developed, common characters or other types of data descriptions cannot be used in binary files or ultra-long texts such as charts and attachments in documents, this requires the backend database to have the ability to store the data. Oracle lob objects can be used to implement this function.

Oracle lob is a data type used to store a large amount of binary and text data (a lob field can store up to 4 GB of data ). Currently, it can be divided into two types: Internal lob and external lob. Internal lob stores data in the database as byte streams. Therefore, many internal lob operations can be involved in transactions, or they can be backed up and restored just like normal data. Oracle8i supports three types of internal lob: blob (binary data), clob (single-byte character data), and nclob (Multi-byte country character data ). The clob and nclob types are suitable for storing ultra-long text data, and the Blob field is suitable for storing a large amount of binary data, such as, video, and audio. Currently, Oracle8i only supports one type of external lob, that is, bfile. In the database, this type only stores the location information of data in the operating system, and the file form other than the data entity exists in the operating system file system. Therefore, the data of this type is read-only and does not participate in transactions. This type helps users manage a large number of externalProgramThe file to be accessed.

PHP Oracle 8 Function Analysis

There are two Oracle Function Extension libraries in PHP. The oracle8 functions allow access to the oracle8 and oracle7 databases. These functions use oracle8 call-interface (oci8 ), you can bind local and global PHP variables to the Oracle location flag. Full support for LOB, file, and rowid allows you to use User-Defined variables, that is, User-Defined Object Classes of your database.

The ocifetchinto function in the oracle8 function library is used to retrieve a row of data records and put them into an array. the syntax of this function is described as follows:

Int ocifetchinto (array & result, int [mode])

The mode parameter can be omitted, and the internal value is oci_num. When accessing Oracle lob, if you want to return a lob object, the mode should be set to oci_assoc + oci_return_lobs.

The ocibindbyname function is used to bind PHP variables to Oracle Objects to establish data communication between PHP and Oracle. The function syntax is described as follows:

Boolean ocibindbyname (INT stmt, string ph_name, mixed & variable, int length, int [type])

The stmt parameter is a string indicator parsed by the Oracle parsing function ociparse. The ph_name parameter is the name of the Oracle return variable to be bound. The & symbol must be added before the parameter variable. The parameter length is the length of the variable. If it is set to-1, the maximum value of the specified variable is used. The parameter type can be omitted. Its values include oci_ B _file (binary file) and oci_ B _cfile (text file) oci_ B _clob, oci_ B _blob, and oci_ B _rowid (rowid. It is worth noting that when using the lob/rowid/bfile data type in oracle8, you must first execute the ocinewdescriptor () function and set the length parameter to-1.

The ocinewdescriptor function is used to initialize the new lob/file description. The function syntax is described as follows:

String ocinewdescriptor (INT connection, int [type])

The type is the same as the type definition in the ocibindbyname function.

Required environment Configuration

Oracle 8 client library is required to use the Oracle 8 function library of PHP. Before using this extension, make sure that you have correctly set Oracle environment variables for oracle users and web daemon users. The environment variables to be set are listed below:

ORACLE_HOME # Oracle installation path
Oracle_sid # Oracle Database ID
LD_LIBRARY_PATH # LD connection LIBRARY PATH
Nls_lang # oralce region (language) settings
Ora_nls33 # ora_nls33 path
Verify that the preceding variables are correct in a Linux environment. The best way is to execute them in the Oracle user and nobody respectively:

# Env

Determine whether the preceding environment variables are consistent based on the output results.

After setting environment variables for Web Server users, you also need to add the Web server users (nobody, WWW) to the Oracle group.

For detailed instructions on oracle8 client and PHP installation settings, refer to the relevant technical manual.

Example

An example of uploading and storing files in the Oracle lob field in PHP is as follows:

// Lob object initialization, get PHP variable pointer

$ Lob = ocinewdescriptor ($ Conn, oci_d_lob );

// Add a record to the relevant file record table, Oracle SQL syntax explanation

$ Stmt = ociparse ($ Conn, "Update t_file set filename = '". $ _ FILES ['fj1'] ['name'].
"', Filetype = '". $ _ FILES ['f1'] ['type']. "', files = empty_blob () Where fileid = ". $ newid. "returning files into: Lob ");

// Bind the lob variable

Ocibindbyname ($ stmt, ': lob', & $ lob,-1, oci_ B _blob );

Ociexecute ($ stmt, oci_default); // execute the statement

If ($ lob-> SaveFile ($ F1) {// save the file submitted by the form to the Oracle database through the lob pointer

Ocicommit ($ conn );

// Upload successfully

} Else {

// Failed to upload

}

Ocifreedesc ($ LOB );

Ocifreestatement ($ stmt );

// End

An example of an application that extracts and downloads files from a database in PHP is as follows:

$ Stmt = ociparse ($ Conn, "select * from T _ file where fileid = $ id ");

Ociexecute ($ stmt );

If (ocifetchinto ($ stmt, $ result, oci_assoc + oci_return_lobs )){

// Output file type information for the browser to determine

Header ("Content-Type:". $ result [filetype]);

// Output file name. The browser prompts whether to open or download the file.

Header ("content-Disposition: attachment; filename =". $ result [filename]);

// For more information about how to use the header function, see PHP manual. For more information about the HTML header, see Internet RFC 2616.

// Output the file stream. Here, the browser obtains the file content and prompts that the file is being downloaded or opened directly.

Echo $ result [files];

}

Ocifreestatement ($ stmt );

// End

The above example is only a key statement of the application, and has passed the verification on the Linux + PHP + Apache + Oracle8i platform. Readers can complete and supplement it according to their own needs.

Conclusion

This article discusses the implementation of PHP's access to Oracle lob, which is only one aspect of PHP's basic application technology in the B/S structure system. The author has compared the execution efficiency of JDBC and PHP-OCI, with JDBC as the Access Engine oas http server (HTTP server as apache2.0) the response speed is inferior to the Web server built on the Linux + aache + PHP-OCI platform, which of course has the outstanding performance of Linux, but it is undeniable that, the combination of PHP and OCI is excellent.

Due to the lack of attempts to combine Oracle lob and PHP oracle8 function libraries, to manage large data files in the Development Information Management System, PHP's functions in this area have not been effectively used. I hope this article will be helpful to programmers who are engaged in PHP database technology but have encountered such difficulties.

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.