PDO obtains the Oraclelob large field. The solution to the problem that cannot be retrieved when the data volume is too large

Source: Internet
Author: User
Recently, in an sns project, PDO is used to connect to the Oracle database and read the Clob field. When the data volume is very large (tens of thousands of characters), a null value is returned, no relevant information was found from the Internet. I worked hard and helped my colleagues to solve the problem. If you don't talk nonsense, post the content below.

Recently, in an sns project, PDO is used to connect to the Oracle database and read the Clob field. When the data volume is very large (tens of thousands of characters), a null value is returned, no relevant information was found from the Internet. I worked hard and helped my colleagues to solve the problem. If you don't talk nonsense, post the content below.

First, create a stored procedure get_clob:
T_name: name of the table to be queried; f_name: name of the field to be queried; u_id: Primary Key of the table, query condition; l_pos: Start position of the truncation; l_amount: truncation length;
Create or replace procedure get_clob (t_name in varchar2, f_name in varchar, u_id in integer, l_pos in integer, l_amount in BINARY_INTEGER, ReturnValue out varchar2) is
Rule_xml clob;
Rochelle buffer varchar2 (3999 );
Rochelle amount _ BINARY_INTEGER;
Begin
Execute immediate 'select' | f_name | 'from' | t_name | 'where id =: 1 'into rule_xml using u_id;
Rochelle amount _: = l_amount;
DBMS_LOB.read (rule_xml, l_amount _, l_pos, l_buffer );
ReturnValue: = l_buffer;
End get_clob;
Then the php processing program:
$ Content = "";
$ Num = 0; // clob Field Length
$ Stmt = $ oracle-> prepare ("select length (content) as num from test where id = $ id ");
If ($ stmt-> execute () {// zsequence is the query Condition
$ Row = $ stmt-> fetch ();
$ Num = $ row ['num'];
}
$ Start = 1; // initialization start position
$ Len = 2500; // capture Length
$ T_name = 'test'; // operation table name
$ F_name = 'content'; // name of the clob field to be queried
While ($ start <= $ num ){
$ Ret = '';
$ SQL = "begin get_clob (?,?,?,?,?,?); End ;";
$ Stmt = $ oracle-> prepare ($ SQL );
$ Stmt-> bindParam (1, $ t_name, PDO: PARAM_STR, 100 );
$ Stmt-> bindParam (2, $ f_name, PDO: PARAM_STR, 100 );
$ Stmt-> bindParam (3, $ id, PDO: PARAM_STR, 100 );
$ Stmt-> bindParam (4, $ start, PDO: PARAM_STR, 100 );
$ Stmt-> bindParam (5, $ len, PDO: PARAM_STR, 100 );
$ Stmt-> bindParam (6, $ ret, PDO: PARAM_STR, 5000 );
$ Stmt-> execute ();
$ Content. = $ ret;
$ Start = $ start + $ len;
}
$ Oracle = null;
The above is the complete solution. This method is not the first one. Most people who have worked on pb projects should be familiar with this operation.
The number of php Oracle operations is still relatively small on the Internet, and there is not much to solve the problem. I will share it with you. There are certainly other good solutions, you are welcome to discuss with us.

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.