Home php教程 php手册 PHP管理Oracle LOB数据的相关办法讲解

PHP管理Oracle LOB数据的相关办法讲解

Jun 13, 2016 am 11:07 AM
oracle php Method us data of Related Know manage explain

我们都知道VARCHAR2也是Oracle公司推荐使用的 类型。但使用VARCHAR2有个问题:最大只能表示4000个字符,也就相当于2000个汉字。如果你的程序中某个字符的值要大于20002个汉字,用 VARCHAR2就不能满足要求了。这时候,你有两个选择,一是用多个VARCHAR2来表示,二是用LOB字段。这里我们来看看第二个办法。

先来大体了解一下Oracle的LOB字段。Oracle的LOB类型分为三种:BLOB,CLOB和BFILE。CLOB称为字符LOB,BLOB和 BFILE是用来存储二进制数据的。CLOB和BLOB的最大长度是4GB,它们把值存放在Oracle数据库中。BFILE和BLOB类似,但它把数据 放在外部的文件中,所以它又称为外部BLOB(External BLOB)。

我想,我们对MYSQL应该都不会陌生。MYSQL中也有类似的数据类型,如TEXT和BLOB。在PHP的 MYSQL函数中,对TEXT/BLOB的操作是直接的,就象其它类型的数据一样。但在Oracle中,情况就不一样了。Oracle把LOB当作一种特 殊的数据类型来处理,在操作上不能用常规的方法。比如,不能在INSERT语句中直接把值插入到LOB字段中,也不能用LIKE进行查找。

下面就通过几个PHP管理Oracle LOB数据的例子来说明如何用PHP的OCI函数来插入,取出和查询LOB数据。

插入

不能直接用INSERT语句向LOB字段中插入值。一般情况下,有如下的几步:

1、先分析一个INSERT语句,返回一个LOB的描述符

2、用OCI函数生成一个本地的LOB对象

3、将LOB对象绑定到LOB描述符上

4、执行INSERT语句

5、给LOB对象赋值

6、释放LOB对象和SQL语句句柄

下面的这个PHP管理Oracle LOB数据的例子是把用户上传的图片文件存放到BLOB(或BFILE中,操作稍有不同)中。首先要建一个表,结构如下:

<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>
Copy after login

如果要实现ID的自动增加,再建一个SEQUENCE:

CREATE SEQUENCE PIC_SEQ;

然后是用来处理数据的PHP程序代码。

<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>
Copy after login

还有一个要注意的地方:LOB字段的值最少要1个字符,所以在save()或savefile()之前,要确保值不能为空。否则,Oracle会出错。

取出

PHP管理Oracle LOB数据对一个LOB中取出数据,有两种办法。一是生成一个LOB对象,然后绑定到一条SELECT语句返回的定位符上,再用LOB对象的load()方法取出 数据;二是直接用PHP的OCIFetch***函数。第一种方法比第二种方法要麻烦得多,所以我直接说说第二种方法。

还是用上面的表。

<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>
Copy after login

这个程序用来显示放在LOB中的数据(图片)。调用方法(假设脚本名是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>
Copy after login

查询

前面已经提了下,PHP管理Oracle LOB数据对于Oracle的LOB字段是不能用LIKE进行匹配的。怎么办呢?其实并不复杂,Oracle有一个匿名的程序包,叫DBMS_LOB,里面有所有的操作LOB所需的过程。

假设有象这样一个表:

<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>
Copy after login

文章的内容放在CONTENT字段中。

现在我们要找出所以内容中包含”PHP中文用户”的文章,可以这么来做:

<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> 
Copy after login

Oracle还提供了许多用来操作LOB数据的过程,如LENGTH, SUBSTR等等。至于它们的详细用法,可以考虑Oracle的开发手册。

关于PHP管理Oracle LOB数据的操作,就说这么多了。由于我接触Oracle时间不长,本文中可能还会有错误,欢迎大家批评指正。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles