Oracle php operations

Source: Internet
Author: User
Tags sql error

<?
// [Warning]: do not modify it without permission
// Configure //------------------------------------------------------------------------------------------
// Configure //------------------------------------------------------------------------------------------
//
// [File name]: c_ora_db.inc
// [For use]: Oracle public function class
// [Operator]: tianyao
//
// [Last modification date]: 2001/05/11 [cxx]
// [Variable definition rules]: 'C _ '= numeric type,' I _ '= integer type, 'n' _' = numeric type, 'l _ '= boolean type, 'A _ '= array type
// Configure //------------------------------------------------------------------------------------------
// Configure //------------------------------------------------------------------------------------------
// ※Db_logon () enables database connection
// ※Db_query () General select
// ※Db_change () general functions for database changes (insert, delete, update)
// ※Db_insert () insert: directly call db_change ()
// ※Db_delete () delete: directly calls db_change ()
// ※Db_update () update directly calls db_change ()
// ※Db_commit () Transaction submission
// ※Db_rollback () Transaction rollback
// ※Db_logoff () disconnects the database.
// Configure //------------------------------------------------------------------------------------------

Class c_ora_db
{

// Configure //------------------------------------------------------------------------------------------
// Variable definition
// Configure //------------------------------------------------------------------------------------------
Var $ C_user = ""; // database username
Var $ C_passwd = ""; // Database Password
Var $ C_db = ""; // Database Name
Var $ I _linkID = 0; // connection handle
Var $ I _effectid = 0; // query handle
Var $ color = ""; // global color
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_logon ()
// For usage: Enable database connection
// Parameter count: None
// Return value: Line handle (integer)
// Standby Note: None
// Configure //------------------------------------------------------------------------------------------
Function db_logon ()
{
$ This-> I _linkID = @ OCILogon ($ this-> C_user, $ this-> C_passwd, $ this-> C_db );
If ($ this-> I _linkID = 0) {AlertExit ('database link failed. Please contact DBA! ');}
Return $ this-> I _linkID;
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_query ($ C_ SQL, $ A_define = "", $ I _start =-1, $ I _end =-1)
// For use: select
// Parameter: $ C_ SQL SQL statement
// $ A_define field to be bound. Array type
// $ I _start starts to retrieve records-1 then retrieve all queried records
// $ I _end
// Return value: Two-dimensional array ($ A_rs)
// Standby note: the value of the corresponding field can be accessed through numbers 0, 1, 2... or the value of the corresponding field can be accessed by querying the field name
// If you use $ A_rs [0] [0], $ A_rs [0] ['name'], or $ A_rs [0] ['name'], you can access the first record NAME. field
// $ I _start and $ I _end are parameters used with paging.
// Configure //------------------------------------------------------------------------------------------
Function db_query ($ C_ SQL, $ A_define = "", $ I _start =-1, $ I _end =-1)
{
If (! $ C_ SQL) {AlertExit ("the parameter is incomplete! ");} // Check parameters

// Connection Detection
If ($ this-> I _linkID = 0) {AlertExit ('database link failed. Please contact DBA! ');}

// Format Detection
$ This-> I _javastid = OCIParse ($ this-> I _linkID, $ C_ SQL );
If (! $ This-> I _effectid) {AlertExit ('SQL format error! Contact the programmer ');}

// If no bound field is specified, it is retrieved from the SQL statement.
If ($ A_define = "")
{
$ A_Cur = explode ("select", $ C_ SQL );
$ A_Cur = explode ("from", $ A_Cur [1]);
$ A_define = explode (",", $ A_Cur [0]);
}

// Bind the database table Field
If (gettype ($ A_define) = "array") // the query column is an array.
{
For ($ I = 0; $ I <count ($ A_define); $ I ++)
{
$ A_define_up [$ I] = trim (strtoupper ($ A_define [$ I]); // uppercase and remove Spaces
}
For ($ I = 0; $ I <count ($ A_define_up); $ I ++)
{
OCIDefineByName ($ this-> I _inclutid, "$ A_define_up [$ I]", & $ A_define [$ I]); // bind
}
}
Elseif (trim ($ A_define) <> "") // only one query Column exists.
{
$ A_define_up = trim (strtoupper ($ A_define ));
OCIDefineByName ($ this-> I _inclutid, "$ A_define_up", & $ A_define );
}

// Execute the bound SQL statement
If (! OCIExecute ($ this-> I _effectid ))
{
Echo "<font color = red> <B> execution Error: </B> </font> SQL Error: <font color = red> $ C_ SQL </font> <br> ";
Return false;
}

$ Lower = 0; // returns the First-dimensional subscript control variable of the Two-dimensional array.
$ Cnt = 0; // start to retrieve the Number ID

// Retrieve records
While (OCIFetchInto ($ this-> I _effectid, & $ cur, OCI_ASSOC ))
{
// Retrieve all queried records
If ($ I _start =-1)
{
If (gettype ($ A_define) = "array") // the query column is an array.
{
For ($ I = 0; $ I <count ($ A_define); $ I ++)
{
If ($ cur [$ A_define_up [$ I] <> $ A_define [$ I])
{
$ A_define [$ I] = $ cur [$ A_define_up [$ I];
}
$ A_rs [$ lower] [$ I] = $ A_define [$ I]; // use a number for access
$ A_rs [$ lower] [$ A_define [$ I] =$ $ A_define [$ I]; // use a smaller access
$ A_rs [$ lower] [$ A_define_up [$ I] = $ A_define [$ I]; // use uppercase for access
}
}
Elseif (trim ($ A_define) <> "") // only one query Column exists.
{

If ($ cur [$ A_define_up] <> $ A_define)
{
$ A_define = $ cur [$ A_define_up];
}
$ A_rs [$ lower] [0] = $ A_define; // use a number to access
$ A_rs [$ lower] [$ A_define] = $ A_define; // access in lower case
$ A_rs [$ lower] [$ A_define_up] = $ A_define; // use larger access
}
$ Lower ++; // subscript plus one
}

// Retrieve the specified record (used with paging)
If ($ I _start <>-1)
{
If ($ cnt >=$ I _start)
{
$ Cnt ++;
If ($ I _end-$ I _start <> 0)
{
$ I _end --;
If (gettype ($ A_define) = "array ")
{
For ($ I = 0; $ I <count ($ A_define_up); $ I ++)
{
If ($ cur [$ A_define_up [$ I] <> $ A_define [$ I])
{
$ A_define [$ I] = $ cur [$ A_define_up [$ I];
}
$ A_rs [$ lower] [$ I] = $ A_define [$ I]; // use a number for access
$ A_rs [$ lower] [$ A_define [$ I] =$ $ A_define [$ I]; // use a smaller access
$ A_rs [$ lower] [$ A_define_up [$ I] = $ A_define [$ I]; // use uppercase for access
}
} Elseif (trim ($ A_define) <> "")
{
If ($ cur [$ A_define_up] <> $ A_define)
{
$ A_define = $ cur [$ A_define_up];
}
$ A_rs [$ lower] [0] = $ A_define; // use a number to access
$ A_rs [$ lower] [$ A_define] = $ A_define; // use a smaller value for access
$ A_rs [$ lower] [$ A_define_up] = $ A_define; // use uppercase for access
}
$ Lower ++;
} Else
{
Break; // If $ I _end-$ I _start = 0, the record is obtained and the while loop exists.
}
} Else
{
$ Cnt ++; // If $ cnt <$ I _start, $ cnt ++
}
}

} // The End Of while

// Release the handle and return the query data (a two-dimensional array)
OCIFreestatement ($ this-> I _effectid );
Return $ A_rs;

} // End of the function
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_change ($ C_ SQL, $ A_bind)
// For use: db change
// Parameter: $ C_ SQL SQL statement
// $ A_bind field to be bound. Array type
// Return value: Boolean
// Standby Note: insert, delete, and update are common.
// Configure //------------------------------------------------------------------------------------------
Function db_change ($ C_ SQL, $ A_bind = "")
{
If (! $ C_ SQL) {AlertExit ("the parameter is incomplete! ");} // Check parameters

// Connection Detection
If ($ this-> I _linkID = "") {AlertExit ("our database is busy. Please connect later! ");}

// Format Detection
$ This-> I _javastid = OCIParse ($ this-> I _linkID, $ C_ SQL );
If (! $ This-> I _effectid) {AlertExit ('SQL format error! Contact the programmer ');}

// Bind
If (gettype ($ A_bind) = "array ")
{
For ($ I = 0; $ I <count ($ A_bind); $ I ++)
{
Global $ A_bind [$ I];
$ A_bind [$ I] = StripSlashes ($ A_bind [$ I]); // remove the backslash character
$ A_bind [$ I] = str_replace ("<? "," <? ", $ A_bind [$ I]); // filter out PHP labels
}
For ($ I = 0; $ I <count ($ A_bind); $ I ++ ){
OCIBindByName ($ this-> I _effectid, ": $ A_bind [$ I]", & $ A_bind [$ I],-1); // bind
}
}
Elseif (trim ($ A_bind) <> "") // It is a character rather than an array.
{
Global $ A_bind;
$ A_bind = StripSlashes ($ A_bind );
$ A_bind = str_replace ("<? "," <? ", $ A_bind); // filter out PHP labels
OCIBindByName ($ this-> I _effectid, ": $ arrBind", & $ A_bind,-1 );
}

// Execute and check whether the task is successful
If (! OCIExecute ($ this-> I _effectid, OCI_DEFAULT ))
{
Echo "<font color = red> <B> execution Error: </B> </font> SQL Error: <font color = red> $ C_ SQL </font> <br> ";
Return false;
}

/* // Return the affected number of rows
Global $ I _changenum;
$ I _changenum = OCINumrows ($ this-> I _assisttid );*/

// Release the handle and return the value
OCIFreeStatement ($ this-> I _effectid );
Return true;
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_delete ($ C_ SQL)
// For use: delete
// Parameter: C_ SQL SQL statement
// Return value: Boolean
// Standby Note: This function is used to call db_change ()
// Configure //------------------------------------------------------------------------------------------
Function db_delete ($ C_ SQL)
{
Return $ this-> db_change ($ C_ SQL );
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_insert ($ C_ SQL, A_bind)
// For use: insert
// Parameter: C_ SQL SQL statement
// A_bind binding
// Return value: Boolean
// Standby Note: This function is used to call db_change ()
// Configure //------------------------------------------------------------------------------------------
Function db_insert ($ C_ SQL, $ A_bind = "")
{
Return $ this-> db_change ($ C_ SQL, $ A_bind );
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_update ($ C_ SQL, A_bind)
// For use: update
// Parameter: C_ SQL SQL statement
// A_bind binding
// Return value: Boolean
// Standby Note: This function is used to call db_change ()
// Configure //------------------------------------------------------------------------------------------
Function db_update ($ C_ SQL, $ A_bind = "")
{
Return $ this-> db_change ($ C_ SQL, $ A_bind );
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_commit ()
// For use: Transaction submission
// Parameter count: None
// Return value: Boolean
// Standby Note: None
// Configure //------------------------------------------------------------------------------------------
Function db_commit ()
{
Return (OCICommit ($ this-> I _linkID ));
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_rollback ()
// For use: Transaction rollback
// Parameter count: None
// Return value: Boolean
// Standby Note: None
// Configure //------------------------------------------------------------------------------------------
Function db_rollback ()
{
Return (OCIRollback ($ this-> I _linkID ));
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
// Function name: db_logoff ()
// For usage: disconnect the database
// Parameter count: None
// Return value: Boolean
// Standby Note: None
// Configure //------------------------------------------------------------------------------------------
Function db_logoff ()
{
Return (OCILogoff ($ this-> I _linkID ));
}
// Configure //------------------------------------------------------------------------------------------

// Configure //------------------------------------------------------------------------------------------
}
?>

Related Article

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.