oracle| Access Summary: This article describes how to use PHP's database access technology to implement the storage of Oracle LOB data objects.
Keywords: php;oracle; LOB; storage;
Introduction
PHP, or "Php:hypertext preprocessor", is a versatile scripting language that is 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 write dynamically generated Web pages quickly, but PHP's functionality is not limited to this. PHP is generally considered to be able to implement complex programming tasks faster and more efficiently, and it is because of its stability and the advantages of taking fewer resources to become the necessary web scripting language to develop B/s structure system, which plays a similar role in middleware, that is, syntax parsing and execution.
ORACLE LOB Data Model
In b/S (browser/server, browser/server) application system, need to store more than simply text information, but also include some pictures and audio-visual data or very long text. For example, the development of a set of official documents system, documents in the charts, attachments, such as 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 store the ability of this data. This functionality is implemented using an Oracle LOB object.
An 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 categories: internal LOB and external lob. An internal LOB stores data as a stream of bytes inside the database. As a result, many of the internal LOB operations can participate in the transaction, or they can be backed up and restored like normal data. Oracle8i supports three types of internal lob:blob (binary data), CLOB (single-byte character data), NCLOB (multibyte country character data). Where the CLOB and NCLOB types apply to storing very long text data, blob word Zhi is used to store large amounts of binary data, such as images, video, audio, and so on. Currently, Oracle8i supports only one external LOB type, the bfile type. Within a database, the type stores only the location information of the data in the operating system, and 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 the transaction. This type can help users manage a large number of files accessed by external programs.
PHP Oracle 8 Function analysis
There are two sets of Oracle function extensions in PHP, where the ORACLE8 function allows access to the Oracle8 and Oracle7 databases, which use Oracle8 call-interface (OCI8) to bind local and global to the Oracle location identifier PHP variables that fully support LOB, FILE, and ROWID, allowing users to use user-defined variables, a custom object class for user databases.
Oracle8 function Ocifetchinto used to retrieve one row of data records into an array, the syntax of the function is described as follows:
int Ocifetchinto (array &result, int [mode])
Where the parameter mode can be omitted, the default value is Oci_num. If you want to return a LOB object when you access an Oracle LOB, mode should be set to Oci_assoc+oci_return_lobs.
The function ocibindbyname is used to bind a PHP variable to an Oracle object 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])
The parameter stmt is a string index which is parsed by the Oracle analytic function Ociparse. Parameter ph_name is the name of the Oracle return variable that needs to be bound; variable must precede the & symbol, table PHP variable address. Parameter length is the length of the variable and, if set to-1, the maximum value of the specified variable variable is used; The parameter type can be omitted with a value of oci_b_file (binary file), Oci_b_cfile (text file), Oci_b_clob (text LOB), Oci_b_blob (bit LOB) and Oci_b_rowid (ROWID). It is noteworthy that the Ocinewdescriptor () function needs to be executed first, and the length parameter must be set to 1 when using the new data type specific to the Oracle8. Lob/rowid/bfile.
function Ocinewdescriptor is used to initialize a new lob/file description. The function syntax is described as:
string ocinewdescriptor (int connection, int [type])
Where the type is the same as the type definition in the Ocibindbyname function.
Required Environment Configuration
Using PHP's ORACLE8 function library requires Oracle8 client libraries. Before using this extension, make sure that you have set Oracle environment variables correctly for both Oracle users and Web daemon users. The environment variables you need to 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 in a Linux environment is to perform them separately under Oracle users and nobody:
# env
According to the output results, the above environment variables are judged to be consistent.
After setting the environment variables 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 Example
Examples of applications that upload and store files in an Oracle LOB field in PHP are as follows:
LOB object initialization, getting PHP variable pointers
$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");
Binding LOB Variables
Ocibindbyname ($stmt, ': Lob ', & $lob,-1, Oci_b_blob);
Ociexecute ($stmt, Oci_default);//EXECUTE statement
if ($lob->savefile ($F 1)) {//Save form submitted file to Oracle database via LOB pointer
Ocicommit ($conn);
Successfully uploaded
}else{
Failed to upload successfully
}
Ocifreedesc ($LOB);
Ocifreestatement ($stmt);
End
Examples of applications that extract and download files from the 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 to judge
Header ("Content-type:". $result [FILETYPE]);
The output file name, and the browser prompts you to open or download the file
Header ("content-disposition:attachment; Filename= ". $result [FILENAME]);
Please refer to Internet RFC 2616 for the use of the header function to refer to the file header information for PHP manual,html.
Output file stream, where the browser gets the contents of the file, prompting you to download or open the file directly
echo $result [FILES];
}
Ocifreestatement ($stmt);
End
The above example is only the key statement of application, and through the verification on the Linux+php+apache+oracle8i platform, the reader can be perfected and supplemented according to his own needs.
Conclusion
This paper discusses the implementation of Oracle LOB access by PHP, which is only one aspect of PHP's basic application technology in B/s structure system. The author has compared the implementation efficiency of both JDBC and PHP-OCI, the OAS HTTP server (HTTP server apache2.0) with JDBC as the access engine is less responsive than the web based on the LINUX+AACHE+PHP-OCI platform Server, which of course has the outstanding performance of Linux, but it is undeniable that the combination of PHP and OCI is very good.
Because of the lack of an attempt to combine Oracle LOB with PHP ORACLE8 function library, PHP has not been used effectively in the development of information management system to realize the management of large data files. I hope this article will be helpful to programmers who are involved in this aspect of PHP database technology.