Current File : /home/inlingua/public_html/sales/dbHandler.class.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 $FILE_HANDLER;
var $ERROR_MSG = '';
var $RETURN_XML;
var $XSLT;
var $SELECTRESULTS;
var $SELECTEDRECORDS = 0;
var $TABLENAME = '';
var $KEYFIELDNAME = '';
var $KEYFIELDVALUE = '';
var $FIELDLIST ;
function __construct() {
$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()) {
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,false,65536);
} else {
$conn = mysqli_connect($this->SERVER,$this->USERNAME,$this->PASSWORD) or die("Connection close");
//$conn=mysqli_connect($DBHostName,$DBUserName,$DBPassword) or die("Connection close");
}
if ((!$conn) || (!mysqli_select_db($conn,$this->DATABASE))) {
$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 (mysqli_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) {
echo "TTTT".$this->LOGFILE;
$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) {
if ((empty($sql)) || (!preg_match("/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 = mysqli_query($conn,$sql);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysqli_error($conn)." - " . date('H:i:s');
$this->debug();
return false;
} else {
$result = mysqli_insert_id($conn);
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) {
$conn=$this->CONNECTION;
if ((!$conn) || (!mysqli_select_db($conn,$this->DATABASE))) {
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
$this->debug();
return false;
} else {
$this->SELECTRESULT = mysqli_query($conn,$sql);
if ($this->SELECTRESULT)
{ $this->SELECTEDRECORDS = mysqli_num_rows($this->SELECTRESULT); }
return $this->SELECTEDRECORDS;
}
}
function SelectNext()
{ if ($this->SELECTEDRECORDS == 0)
{
return false;
}
else {
return mysqli_fetch_assoc($this->SELECTRESULT);
}
}
function SelectAndNext($sql) {
if ((empty($sql)) || (!preg_match("/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 = mysqli_query($conn,$sql);
if ($this->SELECTRESULT)
{ return mysqli_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)) || (!preg_match("/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 = mysqli_query($conn,$sql);
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) {
//echo $sql; exit;
if ((empty($sql)) || (!preg_match("/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 = mysqli_query($conn,$sql);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysql_error($conn)." - " . 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 = mysqli_query($conn,$sql);
if (!$results) {
$this->ERROR_MSG = "\r\n" . mysqli_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()
{
/* echo"<pre>";
print_r($_REQUEST); */
/*echo"<pre>";
print_r($this->FIELDLIST);
exit;*/
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;
$insertClause . "<br>";
return $this->Insert($insertClause);
}
// *** Function to save / add new record to database
function Save()
{
$conn = $this->CONNECTION;
$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 . mysqli_real_escape_string($conn,$fValue[fVal]) . $strDelim . ",";
}
$fldNames = rtrim($fldNames,","); $fldValues = rtrim($fldValues,",");
$fldNames .= ')'; $fldValues .= ')';
$insertClause = "INSERT INTO " . $this->getTableName();
$insertClause .= $fldNames . " VALUES " . $fldValues;
$insertClause.'</br>';
$this->Insert($insertClause);
$result = mysqli_insert_id($conn);
return $result;
}
// *** 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 = mysqli_query($conn,$selQuery);
if (mysqli_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 = mysqli_query($conn,$selQuery);
if (mysqli_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 . "'";
//echo $selQuery;exit;
$selResult = mysqli_query($conn,$selQuery);
if (mysqli_num_rows($selResult) == 0)
{ return false; }
// Update table filed array from select result
$selArray=mysqli_fetch_array($selResult);
/*echo "<pre>";
print_r($selArray);*/
foreach($this->FIELDLIST as $fName => $fValue)
{ $this->setDbField($fName,$selArray[$fName]);
}
return true;
}
function Retrieve_join($query)
{ $conn = $this->CONNECTION;
$selQuery = $query;
$selResult = mysqli_query($conn,$selQuery);
if (mysqli_num_rows($selResult) == 0)
{ return false; }
// Update table filed array from select result
$selArray=mysqli_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)
{
$conn = $this->CONNECTION;
$selQuery = "UPDATE " . $this->TABLENAME . " set ";
foreach($this->FIELDLIST as $fName => $fValue)
{ if ($this->FIELDLIST[$fName]['fSet']) // if ( isset( $_REQUEST[$fName] ))
{ $selQuery .= $fName . '="' . mysqli_real_escape_string($conn,$this->FIELDLIST[$fName][fVal]) . '", '; }
}
$selQuery = rtrim($selQuery,", ");
if ($this->getDbFieldType($kFname) == "S")
{ $selQuery .= " where " . $kFname . "='" . $kFvalue . "'"; }
else
{ $selQuery .= " where " . $kFname . "=" . $kFvalue; }
//echo $selQuery;exit;
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 = mysqli_query($conn,$sql_stmt);
// Determine the total number of rows returned.
$numrows = mysqli_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
### function for send sms###
### Created By Sandeep Dubey###
### Created On 30-11-2016###
function send_sms($mobile,$message)
{
//$url="http://sms1.infocityhosting.com/WebServiceSMS.aspx?User=inlinguasms&passwd=inlingua@1234567&mobilenumber=".$mobile."&message=".$message."&sid=INLGUA&mtype=N";
$url="http://msms.infocityhosting.com/http-api.php?username=inlinguasmsp&password=Welcome1&senderid=INLGUA&route=5&number=".$mobile."&message=".$message;
//echo $url;exit;
$c=curl_init();
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_URL,$url);
$CONTENT=curl_exec($c);
curl_close($c);
//echo $CONTENT;exit;
}
/*##### get Students details ####
function get_detail($ref_no)
{
}*/
} // End of class
?>