Use PHP to access Oracle lob_php Tutorial in BS structure

Source: Internet
Author: User
Tags php database
PHP, or "Php:hypertext preprocessor", is a multi-purpose scripting language widely used for open source and can be embedded in HTML. Its syntax is close to C, Java, and Perl and is easy to learn. The main goal of the language is to allow web developers to quickly write dynamically generated Web pages, however, the functionality of PHP is not limited to this. PHP is generally considered to be faster and more efficient in the implementation of complex programming tasks, and it is because of its more stable and the advantages of less resources to develop the B/s structure system of the necessary Web scripting language, playing the role of similar middleware, that is, syntax parsing and execution.

ORACLE LOB Data Model

In b/S (browser/server, browser/server) applications, the need to store more than just simple text information, but also include some pictures and audio-visual materials or extra-long text. For example, the development of a document system, documents in the chart, attachments and other binary files or extra-long text will not be able to use ordinary characters or other types of data description, which requires the background database to have the ability to store this data. This functionality can be achieved using Oracle LOB objects.

Oracle LOB is a data type used to store large amounts of binary and textual data (a LOB field can store up to 4GB of data). Currently, it is divided into two types: internal LOB and external lob. An internal LOB stores data in the form of a byte stream inside the database. As a result, many operations on internal lobs can participate in transactions, or they can be backed up and restored as normal data. Oracle8i supports three types of internal lob:blob (binary data), CLOB (single-byte character data), NCLOB (multibyte-country character data). The CLOB and NCLOB types are suitable for storing very long text data, and BLOB word MSA is used to store large amounts of binary data, such as video, audio, and so on. Currently, Oracle8i supports only one external LOB type, the bfile type. Within a database, this type stores only the location information of the data in the operating system, while the entity of the data exists in the file system of the operating system as an external file. Thus, the data represented by the type is read-only and does not participate in transactions. This type helps users to manage a large number of files accessed by external programs.

PHP Oracle 8 Function analysis

PHP has two sets of Oracle function extension libraries, where the ORACLE8 function allows access to Oracle8 and Oracle7 databases, which use Oracle8 call-interface (OCI8) to bind local and global to the Oracle location marker PHP variables, which fully support LOB, FILE, and ROWID, allow users to use user-defined variables, which are custom object classes for user databases.

The function ocifetchinto in the ORACLE8 function library is used to retrieve a row of data records into the array, which is described in the following syntax:

int Ocifetchinto (array &result, int [mode])

wherein, the parameter mode can be omitted, the default value is Oci_num. When you access an Oracle LOB, mode should be set to oci_assoc+oci_return_lobs if you want to return the LOB object.

The function ocibindbyname is used to bind PHP variables to Oracle objects to establish data communication between PHP and Oracle, which is described as:

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

Where: Parameter stmt is a string index parsed by the Oracle analytic function Ociparse. The parameter ph_name is the name of the Oracle return variable that needs to be bound, and the parameter variable must precede the & symbol, and the table PHP variable address. The length of the argument is a variable, and if set to 1 uses the maximum value of the specified variable variable, the parameter type can be omitted, its value is oci_b_file (binary), Oci_b_cfile (text file), Oci_b_clob (text LOB), Oci_b_blob (bit LOB) and Oci_b_rowid (ROWID) and several other kinds. It is noteworthy that the Ocinewdescriptor () function needs to be executed when using the new data type lob/rowid/bfile unique to Oracle8, and the length parameter must be set to-1.

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

string ocinewdescriptor (int connection, int [type])

Where type is the same as the type definition in the Ocibindbyname function.

Required Environment Configuration

The Oracle8 client library is required to use PHP's ORACLE8 function library. Before using this extension, make sure that you have set the Oracle environment variables correctly for both Oracle users and Web daemon users. The environment variables that need to be set are listed below:

Oracle_home #ORACLE安装路径
ORACLE_SID # Oracle Database ID
Ld_library_path #LD联接库路径
Nls_lang #ORALCE地区 (language) settings
ORA_NLS33 # ORA_NLS33 Path
The best way to verify that the above variables are correct for the Linux environment is to perform them separately under the Oracle user and nobody:

# env

According to the results of the output, determine whether the above environment variables are consistent.

After setting the environment variable for the Web server user, you also need to add the Web server user (nobody, www) to the Oracle group.

For detailed instructions on ORACLE8 client and PHP installation settings, please refer to the relevant technical manuals.

Application examples

Examples of applications that upload and store files in Oracle LOB fields in PHP are as follows:

LOB object initialization, getting PHP variable pointers

$lob = Ocinewdescriptor ($conn, Oci_d_lob);

Add records to the relevant file record table, ORACLE SQL syntax explained

$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");

Binding LOB Variables

Ocibindbyname ($stmt, ': Lob ', & $lob,-1, Oci_b_blob);

Ociexecute ($stmt, Oci_default);//EXECUTE statement

if ($lob->savefile ($F 1)) {//files submitted by the form are deposited into the Oracle database via LOB pointers

Ocicommit ($conn);

Successfully uploaded

}else{

Failed to upload successfully

}

Ocifreedesc ($LOB);

Ocifreestatement ($stmt);

End

Examples of applications that extract and download files from a database in PHP are 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 browser judgment

Header ("Content-type:". $result [FILETYPE]);

The output file name, which the browser prompts to open or download

Header ("content-disposition:attachment; Filename= ". $result [FILENAME]);

Refer to the file header of PHP manual,html for more information about header functions, please refer to Internet RFC 2616.

Output file stream, where the browser gets the contents of the file, prompting for the file to be downloaded or opened directly

echo $result [FILES];

}

Ocifreestatement ($stmt);

End


The above example is only the key statement of the application, and on the Linux+php+apache+oracle8i platform passed the verification, the reader can be perfected and supplemented according to their own needs.

Conclusion

This paper discusses the implementation of PHP for Oracle LOB access, which is only one aspect of PHP's basic application technology in B/s structure system. The author has compared the execution efficiency between JDBC and PHP-OCI, and the OAS HTTP server (HTTP server for apache2.0) with JDBC as the access engine is less responsive than the web built on the LINUX+AACHE+PHP-OCI platform. Server, which of course has the excellent performance of Linux, but it is undeniable that the combination of PHP and OCI is very good.

Due to the lack of Oracle LOB and PHP ORACLE8 function library, in the development of information management system, in order to achieve the management of large data files, PHP in this aspect of the function has not been effectively utilized, Hopefully this article will be helpful to programmers working on PHP database technology that are experiencing this problem.

http://www.bkjia.com/PHPjc/313861.html www.bkjia.com true http://www.bkjia.com/PHPjc/313861.html techarticle PHP, or "Php:hypertext preprocessor", is a multi-purpose scripting language widely used for open source and can be embedded in HTML. Its syntax is close to C, Java and P ...

  • 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.