<?
//[Warning]: Please do not modify without permission
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
//
// [File name]: c_ora_db.inc
// [Function]: Oracle public function class
// [Author]: Tian Hui
//
// [Last modification date]: 2001/05/11[cxx]
// [Variable definition rules]: 'C_'=Character type,'I_'=Integer type,'N_'=Number type,'L_'=Boolean type,'A_'=Array type
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
// ※db_logon() opens the database connection
// ※db_query() general select
// ※db_change() Universal function for database changes (insert, delete, update)
// ※db_insert() insert, call db_change() directly
// ※db_delete() delete, call db_change() directly
// ※db_update() update, directly call db_change()
// ※db_commit() transaction submission
// ※db_rollback() transaction rollback
// ※db_logoff() disconnects the database connection
//------------------------------------------------ ------------------------------------------
Class c_ora_db
{
//------------------------------------------------ ------------------------------------------
// variable definition
//------------------------------------------------ ------------------------------------------
var $C_user = ""; //Database user name
var $C_passwd = ""; //Database password
var $C_db = ""; //database name
var $I_linkID = 0; //Connection handle
var $I_stmtID = 0; //Query handle
var $color = ""; //global color
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
// Function name: db_logon()
// Function: Open database connection
// Parameters: None
//Return value: connection handle (integer)
// Remarks: None
//------------------------------------------------ ------------------------------------------
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 the DBA!');}
return $this->I_linkID;
}
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
// Function name: db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
// Function: select
// Parameter: $C_sql sql statement
// $A_define fields to be bound. array type
// $I_start starts fetching records - 1 will fetch all the records of the query
// $I_end ends fetching records
//Return value: two-dimensional array ($A_rs)
// Remarks: The value of the corresponding field can be accessed through the numbers 0,1,2....; or the value of the corresponding field can also be accessed by querying the field name
// For example, the NAME field of the first record can be accessed through $A_rs[0][0] or $A_rs[0]['NAME'] or $A_rs[0]['name']
// $I_start, $I_end are parameters used with paging.
//------------------------------------------------ ------------------------------------------
function db_query($C_sql,$A_define="",$I_start=-1,$I_end=-1)
{
if (!$C_sql){AlertExit("Incomplete parameters!");}//Check parameters
//Connection detection
if ($this->I_linkID == 0){AlertExit('Database link failed, please contact the DBA!');}
//Format detection
$this -> I_stmtID = OCIParse($this -> I_linkID,$C_sql);
if (!$this -> I_stmtID){AlertExit('SQL format error! Please contact the programmer');}
//If the bound field is not specified, get it 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 database table fields
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])); //Capitalize and remove spaces
}
for($i=0;$i<count($A_define_up);$i++)
{
OCIDefineByName($this -> I_stmtID,"$A_define_up[$i]",&$$A_define[$i]); //Binding
}
}
elseif(trim($A_define) <> "") //There is only one query column
{
$A_define_up = trim(strtoupper($A_define));
OCIDefineByName($this -> I_stmtID,"$A_define_up",&$$A_define);
}
//Execute the bound SQL statement
if(!OCIExecute($this -> I_stmtID))
{
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 dimension subscript control variable of the two-dimensional array
$cnt = 0; //Start fetching identifier
//Get records
while (OCIFetchInto($this -> I_stmtID,&$cur,OCI_ASSOC))
{
//Get all the records queried
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]; //Access with numbers
$A_rs[$lower][$A_define[$i]] = $$A_define[$i]; //Use smaller access
$A_rs[$lower][$A_define_up[$i]] = $$A_define[$i]; //Access in uppercase
}
}
elseif (trim($A_define) <> "") //There is only one query column
{
if ($cur[$A_define_up] <> $$A_define)
{
$$A_define = $cur[$A_define_up];
}
$A_rs[$lower][0] = $$A_define; //Access with numbers
$A_rs[$lower][$A_define] = $$A_define; //Access in lowercase
$A_rs[$lower][$A_define_up] = $$A_define; //Use larger access
}
$lower++; //Subscript plus one
}
//Retrieve specified records (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]; //Access with numbers
$A_rs[$lower][$A_define[$i]] = $$A_define[$i]; //Use smaller access
$A_rs[$lower][$A_define_up[$i]] = $$A_define[$i]; //Access in uppercase
}
}elseif(trim($A_define) <> "")
{
if ($cur[$A_define_up] <> $$A_define)
{
$$A_define = $cur[$A_define_up];
}
$A_rs[$lower][0] = $$A_define; //Access with numbers
$A_rs[$lower][$A_define] = $$A_define; //Use smaller access
$A_rs[$lower][$A_define_up] = $$A_define; //Access in uppercase
}
$lower++;
}else
{
break; //If $I_end-$I_start=0, it means fetching the record and jumping out of the while loop
}
}else
{
$cnt++; //If $cnt<$I_start,$cnt++
}
}
} //End of while
//Release the handle and return the query data (a two-dimensional array)
OCIFreestatement($this -> I_stmtID);
return $A_rs;
} //End of function
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_change($C_sql,$A_bind)
// Function: db change
// Parameter: $C_sql sql statement
// $A_bind fields to be bound. array type
//Return value: Boolean value
// Note: insert, delete, update are common
//------------------------------------------------ ------------------------------------------
function db_change($C_sql,$A_bind="")
{
if (!$C_sql){AlertExit("Incomplete parameters!");}//Check parameters
//Connection detection
if($this -> I_linkID==""){ AlertExit("Our database is busy, please connect again later!");}
//Format detection
$this -> I_stmtID = OCIParse($this -> I_linkID,$C_sql);
if (!$this -> I_stmtID){AlertExit('SQL format error! Please 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 backslash characters
$$A_bind[$i] = str_replace("<?","< ?",$$A_bind[$i]); //Filter out PHP tags
}
for($i=0;$i<count($A_bind);$i++){
OCIBindByName($this -> I_stmtID, ":$A_bind[$i]", &$$A_bind[$i], -1); //Bind
}
}
elseif(trim($A_bind) <> "") //Not an array, but a character
{
global $$A_bind;
$$A_bind = StripSlashes($$A_bind);
$$A_bind = str_replace("<?","< ?",$$A_bind); //Filter out PHP tags
OCIBindByName($this -> I_stmtID, ":$arrBind", &$$A_bind, -1);
}
//Execute and check whether it is successful
if(!OCIExecute($this -> I_stmtID,OCI_DEFAULT))
{
echo "<font color=red><b>Execution error:</b></font>SQL Error:<font color=red>$C_sql</font><br>";
return false;
}
/*//Return the number of affected rows
global $I_changenum;
$I_changenum = OCINumrows($this -> I_stmtID);*/
//Release the handle and return the value
OCIFreeStatement($this -> I_stmtID);
return true;
}
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_delete($C_sql)
// Function: delete
// Parameter: C_sql sql statement
//Return value: Boolean value
// Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
function db_delete($C_sql)
{
return $this -> db_change($C_sql);
}
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_insert($C_sql,A_bind)
// Function: insert
// Parameter: C_sql sql statement
// A_bind binding
//Return value: Boolean value
// Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
function db_insert($C_sql,$A_bind="")
{
return $this -> db_change($C_sql,$A_bind);
}
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_update($C_sql,A_bind)
// Function: update
// Parameter: C_sql sql statement
// A_bind binding
//Return value: Boolean value
// Note: This function is just for intuitive use, essentially calling db_change()
//------------------------------------------------ ------------------------------------------
function db_update($C_sql,$A_bind="")
{
return $this -> db_change($C_sql,$A_bind);
}
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_commit()
// Function: transaction submission
// Parameters: none
//Return value: Boolean value
// Remarks: None
//------------------------------------------------ ------------------------------------------
function db_commit()
{
return (OCICommit($this->I_linkID));
}
//------------------------------------------------ ------------------------------------------
//------ -------------------------------------------------- ----------------------------------
// Function name: db_rollback()
// Function: transaction rollback
// Parameters: none
//Return value: Boolean value
// Remarks: None
//------------------------------------------------ ------------------------------------------
function db_rollback()
{
return (OCIRollback($this->I_linkID));
}
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
// Function name: db_logoff()
// Function: Disconnect the database connection
// Parameters: none
//Return value: Boolean value
// Remarks: None
//------------------------------------------------ ------------------------------------------
function db_logoff()
{
return (OCILogoff($this->I_linkID));
}
//------------------------------------------------ ------------------------------------------
//------------------------------------------------ ------------------------------------------
}
?>