faqts : Computers : Programming : Languages : PHP : Database Backed Sites : Oracle

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

26 of 31 people (84%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do i store more than 2000 chars in a CLOB using the PHP OCI functions?

Jun 28th, 2005 02:07
Tim Nields, Chris Hall,


answering my own question!

say you have a table:

table "THIS_TABLE"
USER_ID varchar2(30)
BIG_FIELD clob

and the information you want to save to the db is in the vars $user_id 
and $big_field_data.

---start code snippet---

//connect to your database
$conn = OCILogon($db_user, $db_pass);

//well, first you have to create a descriptor
$clob = OCINewDescriptor($conn, OCI_D_LOB);

//setup your query
$query = "insert into THIS_TABLE (USER_ID, BIG_FIELD) values ".
     "('$user_id', EMPTY_CLOB()) ".
     "returning BIG_FIELD into :THE_CLOB";

//then parse your query
$stmt = OCIParse($conn, $query);

//then you have to bind :THE_CLOB to $clob
OCIBindByName($stmt, ":THE_CLOB", &$clob, -1, OCI_B_CLOB);

//then execute your query (OCI_DEFAULT flag is a *MUST HAVE*)
OCIExecute($stmt, OCI_DEFAULT);

//and save the information to the db via the descriptor
$clob->save($big_field_data);

//then commit (again, *MUST HAVE*)
OCICommit($conn);

//free your descriptor and statement handle
OCIFreeDescriptor($clob);
OCIFreeStatement($stmt);

//Disconnect from db
OCILogoff($conn);

---end code snippet---

and you are done.  see the OCI functions in the documentation for a 
description of the flags used in the functions.