Current File : /home/inlingua/www/noida/icentex_noida/dbHandler.class_28_05_13.php |
<?php
class dbHandler {
// Change these variables to your own database settings
var $SERVER = '';
var $DATABASE = '';
var $USERNAME = '';
var $PASSWORD = '';
var $LOGFILE = ""; // full path to debug LOGFILE. Use only in debug mode!
var $LOGGING = false; // debug on or off
var $SHOW_ERRORS = true; // output errors. true/false
var $USE_PERMANENT_CONNECTION = false;
// Do not change the variables below
var $CONNECTION;
var $FILE_HANDLER;
var $ERROR_MSG = '';
var $RETURN_XML;
var $XSLT;
var $SELECTRESULTS;
var $SELECTEDRECORDS = 0;
var $TABLENAME = '';
var $KEYFIELDNAME = '';
var $KEYFIELDVALUE = '';
var $FIELDLIST ;
function dbHandler() {
$this->RETURN_XML = false;
$this->SERVER = $GLOBALS['SERVER'];
$this->DATABASE = $GLOBALS['DATABASE'];
$this->USERNAME = $GLOBALS['USERNAME'];
$this->PASSWORD = $GLOBALS['PASSWORD'];
}
/*
function dbHandler($server,$database,$username,$password) {
$this->RETURN_XML = false;
$this->SERVER = $server;
$this->DATABASE = $database;
$this->USERNAME = $username;
$this->PASSWORD = $password;
}
*/
###########################################
# Function: init
# Parameters: N/A
# Return Type: boolean
# Description: initiates the MySQL Handler
###########################################
function connect() {
$this->logfile_init();
if ($this->OpenConnection()) {
foreach($_REQUEST as $key=>$val)
{
$_REQUEST[$key]=mysql_real_escape_string($_REQUEST[$key]);
}
return true;
} else {
return false;
}
}
###########################################
# Function: OpenConnection
# Parameters: N/A
# Return Type: boolean
# Description: connects to the database
###########################################
function OpenConnection() {
if ($this->USE_PERMANENT_CONNECTION) {
$conn = mysql_pconnect($this->SERVER,$this->USERNAME,$this->PASSWORD);
} else {
$conn = mysql_connect($this->SERVER,$this->USERNAME,$this->PASSWORD);
}
if ((!$conn) || (!mysql_select_db($this->DATABASE,$conn))) {
$this->ERROR_MSG = "\r\n" . "Unable to connect to database - " . date('d-m-y H:i:s') . "<br>";
$this->debug();
return false;
} else {
$this->CONNECTION = $conn;
return true;
}
}
###########################################
# Function: CloseConnection
# Parameters: N/A
# Return Type: boolean
# Description: closes connection to the database
###########################################
function CloseConnection() {
if (mysql_close($this->CONNECTION)) {
return true;
} else {
$this->ERROR_MSG = "\r\n" . "Unable to close database connection - " . date('H:i:s');
$this->debug();
return false;
}
}
###########################################
# Function: logfile_init
# Parameters: N/A
# Return Type: N/A
# Description: initiates the logfile
###########################################
function logfile_init() {
if ($this->LOGGING) {
$this->FILE_HANDLER = fopen($this->LOGFILE,'a') ;
$this->debug();
}
}
###########################################
# Function: logfile_close
# Parameters: N/A
# Return Type: N/A
# Description: closes the logfile
###########################################
function logfile_close() {
if ($this->LOGGING) {
if ($this->FILE_HANDLER) {
fclose($this->FILE_HANDLER) ;
}
}
}
###########################################
# Function: debug
# Parameters: N/A
# Return Type: N/A
# Description: logs and displays errors
###########################################
function debug() {
if ($this->SHOW_ERRORS) {
echo $this->ERROR_MSG;
}
if ($this->LOGGING) {
if ($this->FILE_HANDLER) {
fwrite($this->FILE_HANDLER,$this->ERROR_MSG);
} else {
return false;
}
}
}
###########################################
# Function: Insert
# Parameters: sql : string
# Return Type: integer
# Description: executes a INSERT statement and returns the INSERT ID
###########################################
function Insert($sql) {
//echo $sql;
if ((empty($sql)) || (!eregi("^insert",$sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not an INSERT - " . date('H:i:s');
$this->debug();
return false;
} else {
$conn = $this->CONNECTION;
$results = mysql_query($sql,$conn);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
$this->debug();
return false;
} else {
$result = mysql_insert_id();
return $result;
}
}
}
###########################################
# Function: Select
# Parameters: sql : string
# Return Type: array
# Description: executes a SELECT statement and returns a
# multidimensional array containing the results
# array[row][fieldname/fieldindex]
###########################################
function Select($sql) {
if ((empty($sql)) || (!eregi("^select",$sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
$this->debug();
return false;
} else {
//echo $sql;
$conn = $this->CONNECTION;
$this->SELECTRESULT = mysql_query($sql,$conn);
if ($this->SELECTRESULT)
{ $this->SELECTEDRECORDS = mysql_num_rows($this->SELECTRESULT); }
//echo $sql . "<br>";
return $this->SELECTEDRECORDS;
}
}
function SelectNext()
{ if ($this->SELECTEDRECORDS == 0) { return false;}
else { return mysql_fetch_array($this->SELECTRESULT); }
}
function SelectAndNext($sql) {
if ((empty($sql)) || (!eregi("^select",$sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
$this->debug();
return false;
} else {
$conn = $this->CONNECTION;
$this->SELECTRESULT = mysql_query($sql,$conn);
if ($this->SELECTRESULT)
{ return mysql_fetch_array($this->SELECTRESULT); }
else
{ return false; }
}
}
###########################################
# Function: Update
# Parameters: sql : string
# Return Type: boolean
# Description: executes a REPLACE statement
###########################################
function UpdateInternal($sql) {
if ((empty($sql)) || (!eregi("^update",$sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a REPLACE - " . date('H:i:s');
$this->debug();
return false;
} else {
$conn = $this->CONNECTION;
$results = mysql_query($sql,$conn);
if (!$results) {
$this->ERROR_MSG = "\r\n" . "Error in SQL Statement : ($sql) - " . date('H:i:s');
$this->debug();
return false;
} else {
return true;
}
}
}
###########################################
# Function: DeleteInternal
# Parameters: sql : string
# Return Type: boolean
# Description: executes a DELETE statement
###########################################
function DeleteInternal($sql) {
if ((empty($sql)) || (!eregi("^delete",$sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a DELETE - " . date('H:i:s');
$this->debug();
return false;
} else {
$conn = $this->CONNECTION;
$results = mysql_query($sql,$conn);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
$this->debug();
return false;
} else {
return true;
}
}
}
###########################################
# Function: Query
# Parameters: sql : string
# Return Type: boolean
# Description: executes any SQL Query statement
###########################################
function Query($sql) {
if ((empty($sql)) || (empty($this->CONNECTION))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> - " . date('H:i:s');
$this->debug();
return false;
} else {
$conn = $this->CONNECTION;
$results = mysql_query($sql,$conn);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
$this->debug();
return false;
} else {
return true;
}
}
}
// *** get selected records
function getSelectedRecords()
{ return $this->SELECTEDRECORDS; }
// *** Retrieve database fields from request
function getDbFieldsFromRequest()
{ foreach($this->FIELDLIST as $fName => $fValue)
{ if ( isset( $_REQUEST[$fName] ))
{ $this->setDbField($fName,$_REQUEST[$fName]); }
}
}
// *** Reset the flag which tells whether value is set
function resetValues()
{ foreach($this->FIELDLIST as $fName => $fValue)
{ $this->FIELDLIST[$fName][fSet] = false; }
}
// *** set/get field list array
function setDbFieldList($fList)
{ $this->FIELDLIST = $fList; }
function getDbFieldList()
{ return $this->FIELDLIST; }
// *** set/get a field from field list array
function setDbField($fName,$fValue)
{ $this->FIELDLIST[$fName][fVal] = trim($fValue);
$this->FIELDLIST[$fName][fSet] = true;
}
function getDbField($fName)
{ return $this->FIELDLIST[$fName][fVal]; }
// *** set/get for field type from field list array
function setDbFieldType($fName,$fType)
{ $this->FIELDLIST[$fName][fTyp] = $fType; }
function getDbFieldType($fName)
{ return $this->FIELDLIST[$fName][fTyp]; }
// *** set/get for table name
function setTableName($tbname)
{ $this->TABLENAME = $tbname; }
function getTableName()
{ return $this->TABLENAME; }
// *** set/get for key field name
function setKeyFieldName($kName)
{ $this->KEYFIELDNAME = $kName; }
function getKeyFieldName()
{ return $this->KEYFIELDNAME; }
// *** set/get for key field name
function setKeyFieldValue($kValue)
{ $this->KEYFIELDVALUE = $kValue; }
function getKeyFieldValue()
{ return $this->KEYFIELDVALUE; }
// *** set/get for key field
function setKeyField($kName,$kValue)
{ $this->KEYFIELDNAME = $kName;
$this->KEYFIELDVALUE = $kValue;
}
// *** Function to save / add new record to database
function Save1()
{
$fldNames = ' ('; $fldValues = ' (';
foreach($this->FIELDLIST as $fName => $fValue)
{ $fldNames .= $fName . ",";
// Append string delimitor incase field type is a string
if ($fValue[fTyp] == "S") { $strDelim = "'"; }
elseif($fValue[fTyp] == "F" || $fValue[fTyp] == "N") {$strDelim = "'";}
else { $strDelim = ""; }
$fldValues .= $strDelim . $fValue[fVal] . $strDelim . ",";
}
$fldNames = rtrim($fldNames,","); $fldValues = rtrim($fldValues,",");
$fldNames .= ')'; $fldValues .= ')';
$insertClause = "INSERT INTO " . $this->getTableName();
$insertClause .= $fldNames . " VALUES " . $fldValues;
//echo $insertClause . "<br>";
return $this->Insert($insertClause);
}
// *** Function to save / add new record to database
function Save()
{
$fldNames = ' ('; $fldValues = ' (';
foreach($this->FIELDLIST as $fName => $fValue)
{ $fldNames .= $fName . ",";
// Append string delimitor incase field type is a string
if ($fValue[fTyp] == "S") { $strDelim = "'"; }
elseif($fValue[fTyp] == "F" || $fValue[fTyp] == "N") {$strDelim = "'";}
else { $strDelim = ""; }
$fldValues .= $strDelim .$fValue[fVal] . $strDelim . ",";
}
$fldNames = rtrim($fldNames,","); $fldValues = rtrim($fldValues,",");
$fldNames .= ')'; $fldValues .= ')';
$insertClause = "INSERT INTO " . $this->getTableName();
$insertClause .= $fldNames . " VALUES " . $fldValues;
//echo $insertClause . "<br>";
$this->Insert($insertClause);
}
// *** Function to save / add new record to database
function Exists($kFname,$kFvalue)
{ $conn = $this->CONNECTION;
$selQuery = "SELECT " . $kFname . " FROM " . $this->TABLENAME . " where " . $kFname . "='" . $kFvalue . "'";
$selResult = mysql_query($selQuery,$conn);
if (mysql_num_rows($selResult) == 0)
{ return false; }
return true;
}
function ExistsUpdate($kFname,$kFvalue, $kid, $kidvalue)
{ $conn = $this->CONNECTION;
$selQuery = "SELECT " . $kFname . " FROM " . $this->TABLENAME . " where " . $kFname . "='" . $kFvalue . "' and " . $kid . "!='" . $kidvalue . "'";
$selResult = mysql_query($selQuery,$conn);
if (mysql_num_rows($selResult) == 0)
{ return false; }
return true;
}
// *** Function to save / add new record to database
function Retrieve($kFname,$kFvalue)
{ $conn = $this->CONNECTION;
$selQuery = "SELECT * FROM " . $this->TABLENAME . " where " . $kFname . "='" . $kFvalue . "'";
$selResult = mysql_query($selQuery,$conn);
if (mysql_num_rows($selResult) == 0)
{ return false; }
// Update table filed array from select result
$selArray=mysql_fetch_array($selResult);
foreach($this->FIELDLIST as $fName => $fValue)
{ $this->setDbField($fName,$selArray[$fName]); }
return true;
}
function Retrieve_join($query)
{ $conn = $this->CONNECTION;
$selQuery = $query;
$selResult = mysql_query($selQuery,$conn);
if (mysql_num_rows($selResult) == 0)
{ return false; }
// Update table filed array from select result
$selArray=mysql_fetch_array($selResult);
foreach($selArray as $fName => $fValue)
{ $this->setDbField($fName,$selArray[$fName]); }
return true;
}
// *** Function to update record to database
function Update($kFname,$kFvalue)
{ $selQuery = "UPDATE " . $this->TABLENAME . " set ";
foreach($this->FIELDLIST as $fName => $fValue)
{ if ($this->FIELDLIST[$fName][fSet]) // if ( isset( $_REQUEST[$fName] ))
{ $selQuery .= $fName . '="' .$this->FIELDLIST[$fName][fVal] . '", '; }
}
$selQuery = rtrim($selQuery,", ");
if ($this->getDbFieldType($kFname) == "S")
{ $selQuery .= " where " . $kFname . "='" . $kFvalue . "'"; }
else
{ $selQuery .= " where " . $kFname . "=" . $kFvalue; }
return $this->UpdateInternal($selQuery);
}
// *** Function to delete record to database
function Delete($kFname,$kFvalue)
{ $selQuery = "DELETE FROM " . $this->TABLENAME;
if ($this->getDbFieldType($kFname) == "S")
{ $selQuery .= " where " . $kFname . "='" . $kFvalue . "'"; }
else
{ $selQuery .= " where " . $kFname . "=" . $kFvalue; }
return $this->DeleteInternal($selQuery);
}
function prev123Next($sql_stmt, $limit,$addOn){
// set the $offset variable as global
global $PHP_SELF;
global $offset;
$conn = $this->CONNECTION;
// Execute the query
$numresults = mysql_query($sql_stmt,$conn);
// Determine the total number of rows returned.
$numrows = mysql_num_rows($numresults);
// Make sure that the number of rows returned is greater than zero
if($numrows >= 1) {
// determine if offset has been passed to script, or if offset has been tampered with.
if (empty($offset) || ($offset < 0) || ($offset > $numrows)) {
$offset=0;
}
// Determine if a "PREV" link is necessary - if so, add it to the links array
if (($offset > 0) && ($offset <= $numrows)) {
$prevoffset = $offset - $limit;
$link_array[] = "<a href=\"$PHP_SELF?offset=$prevoffset" . $addOn . "\">Prev</a> \n";
}
// Determine the total number of pages needing links
$pages=intval($numrows/$limit);
// $pages variable now contains integer number of pages needed, unless there is a remainder from division
if ($numrows % $limit) {
// There is a remainder, so add one page
$pages++;
}
for ($i=1; $i<=$pages; $i++) { // loop thru
$newoffset=$limit*($i-1);
if ((intval($offset/$limit)) == (intval($i-1)))
{ $link_array[] = "[$i] \n"; }
else {
$link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset" . $addOn . "\">$i</a> \n";
}
}
// Determine if this is the last page.
if (!(($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
// if not last page give NEXT link
if((($numrows - $offset) > $limit) && ($pages !=1) && ($offset < $numrows)){
$link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset" . $addOn . "\">Next</a><br>\n";
}
}
}else{
; // redirect to error page
}
// return the array containing all of the links.
return $link_array;
}// end function prev123next
} // End of class
?>