DbHost . ":" . $this->DbPort, $this->DbUsername, $this->DbPassword) or die("Could not connect: " . mysql_error());
$this->DbConnection = $link;
return $this->DbConnection;
}
/*--------------------------------------------------------------------------------
Usage : execute($sqlquery)
Input : sqlquery
Returns : Executes the sqlquery
if error displays the sqlstatement.
--------------------------------------------------------------------------------*/
function DbExecute($sqlquery, $saveResult = False) {
$sqlresult = mysql_db_query($this->DbName, $sqlquery, $this->DbConnection);
if (empty($sqlresult)) {echo "DataBase error - " . mysql_error() . "
$sqlquery";}
if(strlen(stristr($sqlquery,"insert into"))>0){$retValue = mysql_insert_id($this->DbConnection);} else {$retValue = $sqlresult;}
if ($saveResult) {$this->PResult = $sqlresult;}
/*For creating log*/
/* if ($this->Log) {
list($querytype) = split (" ", (trim($sqlquery)),2);
if (! in_array (strtolower($querytype), $this->NoLogSQL)) {
//To write Log in DB
if ($this->LogDB) {
//Avoid logging the tbldblog insert command - Avoids looping inside
$this->Log = False;
$fieldValues = array(
"sqlquery"=> addslashes($sqlquery),
"modifiedby"=> $_SESSION['userid'],
"modifiedon"=> TODAY,
);
$this->AddNew();
$this->DbUpdateQuery($this->LogDBTable,$fieldValues);
$this->Log = True;
}
//To write Log in File
}
} commented on 04.11.09-2.23pm wen am going to deploy to client goa */
return $retValue;
}
/*--------------------------------------------------------------------------------
Usage : DbGetField()
Input : sqlquery
Returns : Returns the field requested.
Eg. select sample from
Returns the value from the field sample.
If "select *" is given the value returned will be the first field in the table
--------------------------------------------------------------------------------}*/
function DbGetField($sqlquery) {
$oldresulttype = $this->ResultType;
$this->ResultType = MYSQL_NUM;
$data = $this->DbGetRecord($sqlquery);
$this->ResultType = $oldresulttype;
return $data[0];
}
/*--------------------------------------------------------------------------------
Usage : isAvailable($sqlquery)
Input : sqlquery
Returns : Returns true if the sqlquery returns any row otherwise it returns false
Used at : To check whether the data is there in the database
-------------------------------------------------------------------------------- */
function DbIsAvailable($sqlquery) {
$sqlrows = $this->DbGetField($sqlquery);
if ($sqlrows > 0) {return true;} else {return false;}
}
/*--------------------------------------------------------------------------------
Usage : getRecord(sqlquery)
Input : sqlquery
Returns : Returns a single record from the query executed as a associated array
-------------------------------------------------------------------------------- */
function DbGetRecord($sqlquery) {
$sqlresult = $this->DbExecute($sqlquery);
$data = mysql_fetch_array($sqlresult, $this->ResultType);
if (!empty($this->FieldCase) & is_Array($data)) {$data = array_change_key_case($data,$this->FieldCase);}
return $data;
}
/*--------------------------------------------------------------------------------
Usage : DbGetRowCount(sqlquery)
Input : sqlquery
Returns : Returns a row count of the sqlquery
-------------------------------------------------------------------------------- */
function DbGetRowCount($sqlquery) {
$sqlresult = $this->DbExecute($sqlquery);
$count = mysql_num_fields($sqlresult);
return $count;
}
/*--------------------------------------------------------------------------------
Usage : DbGetResultset(sqlquery)
Input : sqlquery
Returns : Returns a row count of the sqlquery
-------------------------------------------------------------------------------- */
function DbGetResultset($sqlquery) {
$sqlresult = $this->DbExecute($sqlquery);
return $sqlresult;
}
/*--------------------------------------------------------------------------------
Usage : DbGetResultset(sqlquery)
Input : sqlquery
Returns : Returns a row count of the sqlquery
-------------------------------------------------------------------------------- */
function DbGetColumnName($resultset,$index) {
$colName = mysql_field_name($resultset, $index);
return $colName;
}
/*--------------------------------------------------------------------------------
Usage : DbFetchRecord()
Input : Result (optional), If result not passed the PResult should be available
Returns : Returns a single record from the query executed as a associated array
Ver : >= 4.0
-------------------------------------------------------------------------------- */
function DbFetchRecord($result="") {
if (empty($result)) {$result = $this->PResult;}
$data = mysql_fetch_array($result, $this->ResultType);
if (is_Array($data)) {return array_change_key_case($data,$this->FieldCase);} else {return False;}
}
function DbUpdateQuery() {
//Get entered aruguments
$argsArray = func_get_args();
$tableName = $argsArray[0];
$fieldValues = $argsArray[1];
$whereinfo = $argsArray[2];
if(!(is_array($fieldValues))) {$fieldValues = $this->FieldValues;}
if(empty($whereinfo)) {$whereinfo = $this->WhereInfo;}
//Build Query
if ($this->AddNew) {
//Insert a different primary key if only ServerCount is more than 1
if ($this->ServerCount > 1) {
$pkey = $this->DbGetPrimaryKey($tableName);
if (empty($fieldValues[$pkey])) {
$MaxPKey = $this->DbGetField("select max($pkey) from " . $tableName);
$MulFactor = $this->ServerNo * $this->ServerCount;
$stepNo = ceil($MaxPKey / $this->ServerCount);
$fieldValues[$pkey] = ($stepNo * $this->ServerCount) + $this->ServerNo;
}
}
$fieldArray = array_keys($fieldValues);
$valueArray = array_values($fieldValues);
$sqlQuery = "insert into ". $tableName ."(".implode(",",$fieldArray).") values ('".implode("','",$valueArray)."')";
$this->AddNew = False;
}
else {
//Arrange the key and values for updation
foreach ($fieldValues as $key => $value) {$updateData[] = "$key = '" . trim($value) . "'";}
$sqlQuery = "update ". $tableName . " set " . implode(",",$updateData);
if (!empty($whereinfo)) {$sqlQuery .= " where " . $whereinfo;}
}
$retval = $this->DbExecute($sqlQuery);
return $retval;
}
//Given the table name returns the primary key.
//Added in 4.0
function DbGetPrimaryKey($tblname) {
$query = "select * from " . $tblname;
$result = $this->DbExecute($query);
$fieldsCount = mysql_num_fields($result);
/*Get Field details*/
for ($j=0; $j < $fieldsCount; $j++) {
$fieldName[$j] = mysql_field_name($result, $j);
$flags = mysql_fetch_field($result, $j);
if ($flags->primary_key == 1) {$pkey = $fieldName[$j]; $j = $fieldsCount;}
}
return $pkey;
}
function AddValue($fieldname,$fieldvalue="") {
$this->FieldValues[$fieldname] = $fieldvalue;
}
function AddValues($fieldValues) {
$this->FieldValues = array_merge ($this->FieldValues, $fieldValues);
}
function AddNew() {
$this->AddNew = True;
}
function ClearValue(){
$this->FieldValues = array();
}
function DbGetMultipleRecords($sqlquery, $key=""){
$sqlretval = $this->DbExecute($sqlquery);
$i = 0;
$rowCount = 0;
$altcount = 0;
if ($this->AltKey) {
$rowCount = count($this->AltValues);
$altArray = $this->AltValues;
}
while ($data = mysql_fetch_array($sqlretval, $this->ResultType)) {
if ($this->ShowRowNo){$data[$this->ShowRowNo] = $i+1;}
//For setting the alternate values.
if ($rowCount){
$data[$this->AltKey] = $altArray[$altcount];
$altcount = $altcount + 1;
if ($altcount >= $rowCount) {$altcount = 0;}
}
if (strcasecmp($key, "FirstVal")) {
if (!strcasecmp($key, "Num")) {$keyvalue = $i;} else {$keyvalue = $key . $i;}
if (!empty($this->FieldCase) & is_Array($data)) {$dataMultiple[$keyvalue] = array_change_key_case($data,$this->FieldCase);} else {$dataMultiple[$keyvalue] = $data;}
}
else {$dataMultiple[$data[0]] = $data[1];} //Single Dimensional Array
$i++;
}
return $dataMultiple;
}
function DbGetMultipleFields($sqlquery, $key="LOOP"){
$sqlretval = $this->DbExecute($sqlquery);
$i = 0;
while ($data = mysql_fetch_array($sqlretval, $this->ResultType)) {
if ($this->ShowRowNo){$data[$this->ShowRowNo] = $i+1;}
if (strcasecmp($key, "FirstVal")) {
if (!strcasecmp($key, "Num")) {$keyvalue = $i;} else {$keyvalue = $key . $i;}
if (!empty($this->FieldCase) & is_Array($data)) {$dataMultiple[$keyvalue] = array_change_key_case($data,$this->FieldCase);} else {$dataMultiple[$keyvalue] = $data;}
}
else {
$single = true;
$dataMultiple0[$i] = $data[0];
$dataMultiple1[$i] = $data[1];
$dataMultiple2[$i] = $data[2];
} //Singe Dimensional Array
$i++;
}
if ($single){
$dataMultiple =array($dataMultiple0,$dataMultiple1,$dataMultiple2);
}
return $dataMultiple;
}
function DbClose() {
mysql_close($this->DbConnection);
}
function DbFreeResult() {
mysql_free_result($this->PResult);
}
function SetHost($host) {
$this->DbHost = $host;
}
function SetPort($port) {
$this->DbPort = $port;
}
function SetUsername($username) {
$this->DbUsername = $username;
}
function SetPassword($password) {
$this->DbPassword = $password;
}
function SetFieldCase($caseval) {
$this->FieldCase = $caseval;
}
function SetResultType($restype) {
$this->ResultType = $restype;
}
function SetShowRowNo($rownotxt) {
$this->ShowRowNo = $rownotxt;
}
function SetAltValues($altkey, $altvalues) {
$this->AltValues = $altvalues;
$this->AltKey = $altkey;
}
function GetVersion() {
return $this->Version;
}
}
?>