


Explanation of relevant methods for managing Oracle LOB data with PHP_PHP Tutorial
We all know that VARCHAR2 is also the type recommended by Oracle. But there is a problem with using VARCHAR2: it can only represent a maximum of 4000 characters, which is equivalent to 2000 Chinese characters. If the value of a certain character in your program is greater than 20,002 Chinese characters, VARCHAR2 cannot meet the requirements. At this time, you have two options, one is to use multiple VARCHAR2 to represent it, and the other is to use LOB fields. Here we take a look at the second method.
First, let’s have a general understanding of Oracle’s LOB fields. Oracle's LOB types are divided into three types: BLOB, CLOB and BFILE. CLOB is called character LOB, BLOB and BFILE are used to store binary data. The maximum length of CLOB and BLOB is 4GB, and they store values in the Oracle database. BFILE is similar to BLOB, but it places data in an external file, so it is also called external BLOB (External BLOB).
I think we are all familiar with MYSQL. There are similar data types in MYSQL, such as TEXT and BLOB. In PHP's MYSQL function, operations on TEXT/BLOB are direct, just like other types of data. But in Oracle, the situation is different. Oracle treats LOBs as a special data type and cannot use conventional methods for operations. For example, you cannot directly insert values into LOB fields in the INSERT statement, nor can you use LIKE to search.
The following uses several examples of PHP management of Oracle LOB data to illustrate how to use PHP's OCI function to insert, retrieve and query LOB data.
Insert
You cannot directly use the INSERT statement to insert values into LOB fields. Generally, there are the following steps:
1. First analyze an INSERT statement and return a LOB descriptor
2. Use the OCI function to generate a local LOB object
3. Bind the LOB object to the LOB descriptor
4. Execute the INSERT statement
5. Assign a value to the LOB object
6. Release the LOB object and SQL statement handle
The following example of PHP managing Oracle LOB data is to store image files uploaded by users into BLOB (or BFILE, the operation is slightly different). First, create a table with the following structure:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TABLE PICTURES ( </span></span></li> <li><span>ID NUMBER, </span></li> <li class="alt"><span>DESCRIPTION VARCHAR2(100), </span></li> <li><span>MIME VARCHAR2(128), </span></li> <li class="alt"><span>PICTURE BLOB </span></li> <li><span>); </span></li> </ol>
If you want to realize the automatic increase of ID, create another SEQUENCE:
CREATE SEQUENCE PIC_SEQ;
Then there is the PHP program code used to process the data.
<ol class="dp-xml"> <li class="alt"><span><span>< ?php </span></span></li> <li><span>//建立Oracle数据库连接 </span></li> <li class="alt"> <span>$</span><span class="attribute">conn</span><span> = </span><span class="attribute-value">OCILogon</span><span>($user, $password, $SID); </span> </li> <li><span>//提交SQL语句给Oracle </span></li> <li class="alt"><span>//在这里要注意的两点:一是用EMPTY_BLOB()函数。这是Oracle的内部函数,<br>返回一个LOB的定位符。在插入LOB时,只能用这个办法先 生成一个空的LOB定<br>位符,然后对这个定位符进行操作。EMPTY_BLOB()函数是针对BLOB类型的,<br>对应于CLOB的是 EMPTY_CLOB()。二是RETURNING后面的部分,把picture<br>返回,让PHP的OCI函数能够处理。 </span></li> <li> <span>$</span><span class="attribute">stmt</span><span> = </span><span class="attribute-value">OCIParse</span><span>($conn,”INSERT INTO PICTURES (id, description, picture) </span> </li> <li class="alt"><span>VALUES (pic_seq.NEXTVAL, ‘$description’, ‘$lob_upload_type’<br>, EMPTY_BLOB()) RETURNING picture INTO ICTURE”); </span></li> <li><span>//生成一个本地LOB对象的描述符。注意函数的第二个参数:OCI_D_LOB,<br>表示生成一个LOB对象。其它可能的还有OCI_D_FILE和OCI_D_ROWID,<br>分别对应于BFILE和ROWID对象。 </span></li> <li class="alt"> <span>$</span><span class="attribute">lob</span><span> = </span><span class="attribute-value">OCINewDescriptor</span><span>($conn, OCI_D_LOB); </span> </li> <li><span>//将生成的LOB对象绑定到前面SQL语句返回的定位符上。 </span></li> <li class="alt"><span>OCIBindByName($stmt, ‘:PICTURE’, &$lob, -1, OCI_B_BLOB); </span></li> <li><span>OCIExecute($stmt); </span></li> <li class="alt"><span>//向LOB对象中存入数据。因为这里的源数据是一个文件,所以直接用LOB<br>对象的savefile()方法。LOB对象的其它方法还有:save()和load(),<br>分别用来保存和取出数据。但BFILE类型只有一个方法就是save() </span></li> <li><span>if($lob->savefile($lob_upload)){ </span></li> <li class="alt"><span>OCICommit($conn); </span></li> <li><span>echo “上传成功< br>”; </span></li> <li class="alt"><span>}else{ </span></li> <li><span>echo “上传失败< br>”; </span></li> <li class="alt"><span>} </span></li> <li><span>//释放LOB对象 </span></li> <li class="alt"><span>OCIFreeDesc($lob); </span></li> <li><span>OCIFreeStatement($stmt); </span></li> <li class="alt"><span>OCILogoff($conn); </span></li> <li><span>?> </span></li> </ol>
There is another thing to note: the value of the LOB field must be at least 1 character, so before save() or savefile(), make sure the value cannot be empty . Otherwise, Oracle will make an error.
Retrieve
PHP manages Oracle LOB data. There are two ways to retrieve data from a LOB. One is to generate a LOB object, then bind it to the locator returned by a SELECT statement, and then use the load() method of the LOB object to retrieve the data; the other is to directly use PHP's OCIFetch*** function. The first method is much more troublesome than the second method, so I will talk about the second method directly.
Still use the table above.
<ol class="dp-xml"> <li class="alt"><span><span>< ?php </span></span></li> <li> <span>$</span><span class="attribute">conn</span><span> = </span><span class="attribute-value">OCILogon</span><span>($user, $password, $SID); </span> </li> <li class="alt"> <span>$</span><span class="attribute">stmt</span><span> = </span><span class="attribute-value">OCIParse</span><span>($conn,”SELECT * <br>FROM PICTURES WHERE </span><span class="attribute">ID</span><span>=$pictureid”); </span> </li> <li><span>OCIExecute($stmt); </span></li> <li class="alt"><span>//秘密就在PCIFetchInfo的第三个参数上:<br>OCI_RETURN_LOBS。第三个参数是FETCH的模式,<br>如果OCI_RETURN_LOBS,就直接把LOB的值放到结<br>果数组中,而不是LOB定位符,也就不用LOB对象的load()方法了。 </span></li> <li><span>if (OCIFetchInto($stmt, $result, OCI_ASSOC+OCI_RETURN_LOBS)) </span></li> <li class="alt"><span>{ </span></li> <li><span>echo “Content-type: ” . StripSlashes($result[MIME]); </span></li> <li class="alt"><span>echo StripSlashes($result[PICTURE]); </span></li> <li><span>} </span></li> <li class="alt"><span>OCIFreeStatement($stmt); </span></li> <li><span>OCILogoff($conn); </span></li> <li class="alt"><span>?> </span></li> </ol>
This program is used to display data (pictures) placed in LOB. Calling method (assuming the script name is getpicture.php):
<ol class="dp-xml"><li class="alt"><span><span>< IMG </span><span class="attribute">SRC</span><span>=”getpicture.php?</span><span class="attribute">pictureid</span><span>=</span><span class="attribute-value">99</span><span>″ <br></span><span class="attribute">ALT</span><span>=”放在Oracle LOB中的图片”> </span></span></li></ol>
Query
As mentioned before, PHP manages Oracle LOB Data cannot be matched using LIKE for Oracle's LOB fields. What to do? In fact, it is not complicated. Oracle has an anonymous package called DBMS_LOB, which contains all the processes required to operate LOB.
Suppose there is a table like this:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TABLE ARTICLES ( </span></span></li> <li><span>ID NUMBER, </span></li> <li class="alt"><span>TITLE VARCHAR2(100), </span></li> <li><span>CONTENT CLOB </span></li> <li class="alt"><span>); </span></li> </ol>
The content of the article is placed in the CONTENT field.
Now we want to find all the articles that contain "PHP Chinese users" in the content. You can do this:
<ol class="dp-xml"> <li class="alt"><span><span>< ?php </span></span></li> <li> <span>$</span><span class="attribute">conn</span><span> = </span><span class="attribute-value">OCILogon</span><span>($user, $password, $SID); </span> </li> <li class="alt"><span>//WHERE子句中用了DBMS_LOB.INSTR过程。它有四个参数,<br>前面两个分别表示LOB的定位符(可以直接用字段表示)<br>和要查找的字符串;后面两个分别表示开始的偏移量和出现的次数。<br>要注意的是必须判断它的返回值,也就是要大于0。 </span></li> <li> <span>$</span><span class="attribute">stmt</span><span> = </span><span class="attribute-value">OCIParse</span><span>($conn,”SELECT * FROM ARTICLES <br>WHERE DBMS_LOB.INSTR(CONTENT, ‘PHP中文用户’, 1, 1) > 0″); </span> </li> <li class="alt"><span>OCIExecute($stmt); </span></li> <li><span>if (OCIFetchInto($stmt, $result, OCI_ASSOC+OCI_RETURN_LOBS)) </span></li> <li class="alt"><span>{ </span></li> <li><span>… </span></li> <li class="alt"><span>} </span></li> <li><span>OCIFreeStatement($stmt); </span></li> <li class="alt"><span>OCILogoff($conn); </span></li> <li><span>?> </span></li> </ol><br>
Oracle also provides many processes for operating LOB data. Such as LENGTH, SUBSTR, etc. As for their detailed usage, you can consider Oracle's development manual.
That’s all about the operation of PHP to manage Oracle LOB data. Since I haven't been in contact with Oracle for a long time, there may be errors in this article. Everyone is welcome to criticize and correct me.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
