Login   Register  
PHP Classes
elePHPant
Icontem

File: sql.cls.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Md. Shahadat Hossain Khan Razon  >  SQL Handling Functions  >  sql.cls.php  >  Download  
File: sql.cls.php
Role: Class source
Content type: text/plain
Description: SQL Function Class
Class: SQL Handling Functions
Execute common MySQL database SQL queries
Author: By
Last change: add some function specially auto insert update function
Date: 3 years ago
Size: 29,867 bytes
 

Contents

Class file image Download
<?php
/*
 * class name : sqlFunctions
 * developed by : Md. Shahadat Hossain Khan Razon
 * version : 1.00
 * date : May 17, 2006
 * dependency : none
 * homepage : http://360.yahoo.com/razonklnbd
 * email : razonklnbd@yahoo.com
 * developed for: http://www.squarepharma.com.bd
 */


/*
 * change log
 *
 * 20110825: fix an internal bug
 * 20110818: handle error with debug flag
 * 20100111: add new function to set core class instance any time "setCoreClassInstance($pi_core_class)"
 * 20080527: add a new function for safe string with real escape functionality
 * 20080525: fixed but for link identifier at mysql_query [runQuery]
 * 20080522: implement connection identifier with internal $this->curlink variable
 * 20080519: fixed bug to implement mysql_real_escape_string with stripslashes if magic quote enabled checking
 *
 */


/*
	var $defaultdb=NULL;
	var $dbhost=NULL;
	var $dbuser=NULL;
	var $dbpass=NULL;
	var $curlink=false;
	var $curdatabase='';
	var $curtable='';
	var $clsCore;
	var $lastSQLquery;
	var $lastSuccessfulSql;
	var $lastInsertedID;
	var $lastAffectedRows=0;

	function __construct($p_core_class, $p_default_database='', $p_cur_link=false){
	function setCoreClassInstance($pi_core_class){
	function setConnection($p_conn){
	function setMyDatabase($p_dbname){
	function setCurrentTable($p_table){
	function openMyConn($p_dbhost='', $p_dbuser='', $p_dbpass=''){
	function openMyDatabase($p_dbname=''){
	function openMyDatabaseX($p_dbhost, $p_dbuser, $p_dbpass, $p_dbname){
	function mysql_current_db(){
	function isDbOpened($p_dbname=''){
	function isTableExists($p_tablename=NULL){
	
//	 * $p_type=0	: unspecified or dynamic
//	 * $p_type=1	: only insert operation
//	 * $p_type=2	: only update operation
	function sqlInsertUpdate($p_fields, $p_pk_value='', $p_pk_name='', $p_table='', $p_pk_remove=true, $p_type=0){
	function getTotalRows($p_rs=NULL){
//	usage				: total number of rows in value using sql count() function
	function getTotalRowsX($p_table_name, $p_condition=NULL){
	function getInsSql($p_fields, $p_table=NULL){
	function getUpdtSql($p_fields, $p_condition, $p_table=''){
	function build_sql($p_fld, $p_val, $p_condition='and'){
	function getRowsVal($p_sql, $p_col_idx=-1){
//	usages				: return array fetch with assoc mode
	function getRowsValX($p_sql, $p_rwnum=0){
	function getMysqlDtTm($p_dt=0){
//	usage: isValExist($kwrd, 'keyword', 'resume_keywords', true, "and `skcid`='$skcid'")
	function isValExist($p_value, $p_field_name='id', $p_table=NULL, $p_case_ignored = true, $p_extra_condition = NULL){
	function sqlFetchAssoc($p_rs){
	function sqlFetchRow($p_rs){
	function sqlFetchObject($p_rs){
	function getLastExecutedQuery(){
	function getLastSuccessfulQuery(){
	function getLastInsertedID(){
	function runQuery($p_sql, $p_lnk_identifier=NULL){
	function getRows($p_idfieldname, $p_sql, $p_lnk_identifier=NULL){
	function getArrWithIdAsKey($p_sql, $p_lnk_identifier=NULL){
	function sqlSafeString($p_val, $p_real_escaping_enabled=true) {
	function isValidDateTime($dateTime){
	function __destruct(){
*/

if(!defined('no')) define('no', 0, true);
if(!defined('yes')) define('yes', 1, true);

class sqlFunctions{
	var $defaultdb=NULL;
	var $dbhost=NULL;
	var $dbuser=NULL;
	var $dbpass=NULL;
	var $curlink=false;
	var $curdatabase='';
	var $curtable='';
	var $clsCore;
	var $lastSQLquery;
	var $lastSuccessfulSql;
	var $lastInsertedID;
	var $lastAffectedRows=0;
	var $lstSRWCLID=true; // Last sql query run with classes link identifier i.e. $this->curlink
	var $myrs;
	var $thisdb=false;
	var $updtFlag=false;
	var $ensureSafeData=true;
	var $dbSafeOpenFl=false;
	
	static $curSystemTime=NULL;

	var $error_level=1; # 0: no error output at all - just save error into class variable, 1: no error is shows to bowser but with comment (default), 2: all error shows and halt process
	var $errors;
	var $error_fl=false;

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function __construct($p_core_class=NULL, $p_default_database='', $p_cur_link=false){
		if(isset($p_core_class)){
			$this->clsCore=$p_core_class;
			sqlFunctions::$curSystemTime=$this->clsCore->getMyCurrentTime();
		}
		if(strlen($p_default_database)>0) $this->defaultdb=$p_default_database;
		if(strlen($this->defaultdb)>0) $this->curdatabase=trim($this->defaultdb);
		if($p_cur_link){
			$this->curlink=$p_cur_link;
			$this->curdatabase=trim($this->defaultdb);
		}
		$this->ensureSafeData=true;
		return true;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20110818
	last modified date	: 20110818
*/
	function showSQLError($p_err_msg, $p_error_type=E_USER_ERROR){
		$this->error_fl=true;
		$err_msg=$p_err_msg.' [Error Type # '.$p_error_type.']';
		$errlvl=$this->error_level;
		switch($errlvl){
			case 0:
				$this->errors[]=$err_msg;
				break;
			case 1:
				echo '<!-- '.htmlentities($err_msg).' -->';
				break;
			case 2:
			default:
				trigger_error($p_err_msg, $p_error_type);
		}
		return true;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20100111
	last modified date	: 20100111
*/
	function setCoreClassInstance($pi_core_class){
		if(isset($p_core_class)){
			$this->clsCore=$p_core_class;
			$this->clsCore->setMySqlInstance($this);
		}
		return true;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20080522
	last modified date	: 20080522
*/
	//	Please use this function before every module running of this class
	function setConnection($p_conn){
		if(!is_resource($p_conn)) $this->showSQLError('<h3>Invalid Resource Identifier</h3>'.mysql_error(), E_USER_ERROR);
		$this->dbSafeOpenFl=false;
		$this->curlink=$p_conn;
		return true;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20091201
*/
	function setMyDatabase($p_dbname, $p_set_through_sql=true){
		return $this->openMyDatabase($p_dbname, $p_set_through_sql);
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	//	Please use this function before every module running of this class
	function setCurrentTable($p_table){
		$this->curtable=trim($p_table);
		return true;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20080522
*/
	function openMyConn($p_dbhost=NULL, $p_dbuser=NULL, $p_dbpass=NULL, $p_pconnect=true){
		$f_dbhost=$this->dbhost;
		$f_dbuser=$this->dbuser;
		$f_dbpass=$this->dbpass;
		if(isset($p_dbhost) && strlen($p_dbhost)>0) $f_dbhost=$p_dbhost;
		if(isset($p_dbuser) && strlen($p_dbuser)>0) $f_dbuser=$p_dbuser;
		if($f_dbpass!=$p_dbpass) $f_dbpass=$p_dbpass;
		if(!isset($f_dbhost) || strlen($f_dbhost)<=0) $this->showSQLError('<h3>NULL Database Host</h3>'.mysql_error(), E_USER_ERROR);
		if(!isset($f_dbuser) || strlen($f_dbuser)<=0) $this->showSQLError('<h3>NULL Database User</h3>'.mysql_error(), E_USER_ERROR);
		if(!isset($f_dbpass) || strlen($f_dbpass)<=0) $this->showSQLError('<h3>NULL Database Password</h3>'.mysql_error(), E_USER_ERROR);
		if(!$this->curlink || $f_dbhost!=$this->dbhost || $f_dbuser!=$this->dbuser || $f_dbpass!=$this->dbpass){
			$func2conn=(true==$p_pconnect?'mysql_pconnect':'mysql_connect');
			## we can't use @ sign cause if we found error then we will handle error with a message
##			if(true==$p_pconnect) $f_lnk=mysql_pconnect($f_dbhost, $f_dbuser, $f_dbpass);
##			else $f_lnk=mysql_connect($f_dbhost, $f_dbuser, $f_dbpass);
			$f_lnk=$func2conn($f_dbhost, $f_dbuser, $f_dbpass);
			if($f_lnk){
				$this->curlink=$f_lnk;
				$this->dbhost=$f_dbhost;
				$this->dbuser=$f_dbuser;
				$this->dbpass=$f_dbpass;
			}else $this->showSQLError('<h3>Cannot open connection..</h3><div>Host: '.$f_dbhost.', User: '.$f_dbuser.'<!-- , Password: '.substr($f_dbpass, 0, 3).'... --></div>'.mysql_error(), E_USER_ERROR);
		}
		if(isset($this->curlink) && is_resource($this->curlink)) return $this->curlink;
		return false;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20091201
*/
	function openMyDatabase($p_dbname=NULL, $p_open_db=true){
		if(strlen($p_dbname)>0) $f_dbname=trim($p_dbname);
		elseif(strlen($this->curdatabase)>0) $f_dbname=$this->curdatabase;
		elseif(strlen($this->defaultdb)>0) $f_dbname=trim($this->defaultdb);
		if(!is_resource($this->curlink)) $this->openMyConn();
		if($this->curlink && strlen($f_dbname)>0){
			if(true===$p_open_db){
				if(!$this->isDbOpened($f_dbname)){
					$this->thisdb=mysql_select_db($f_dbname, $this->curlink) or die('Could not select database!');
					if($this->thisdb) $this->curdatabase=$f_dbname;
					return $this->thisdb;
				}
			}else $this->curdatabase=$f_dbname;
			return true;
		}
		return false;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20090504
	last modified date	: 20071030
*/
	function openMyDatabaseX($p_dbhost, $p_dbuser, $p_dbpass, $p_dbname){
		$rtrn=$this->openMyConn($p_dbhost, $p_dbuser, $p_dbpass);
		if(false!==$rtrn) $this->openMyDatabase($p_dbname);
		if(false!==$rtrn) $this->dbSafeOpenFl=true;
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function mysql_current_db($p_return_through_sql=true){
		if(true===$p_return_through_sql){
			if(!isset($this->curlink)) trigger_error('<h3>No Database Connection!</h3>'.mysql_error(), E_USER_ERROR);
			$r=$this->runQuery('SELECT DATABASE()') or die('<h3>Cannot select database..</h3>'.mysql_error());
			if($r) $v=mysql_result($r,0);
		}else $v=$this->curdatabase;
		return $v;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function isDbOpened($p_dbname=NULL){
		if(strlen($p_dbname)>0){
			$v_dbname=$this->mysql_current_db();
			if($v_dbname==$p_dbname) return true;
		}
		return false;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20080110
	last modified date	: 20080110
*/
	function isTableExists($p_tablename=NULL){
		$bOk=false;
		if(isset($this->curtable) && @strlen($this->curtable)>0) $tblnm=$this->curtable;
		if(isset($p_tablename)>0) $tblnm=$p_tablename;
		if(isset($tblnm)){
			$rs=$this->runQuery('SHOW TABLES');
			while(list($tname)=$this->sqlFetchRow($rs)){
				if($tname==$p_tablename) $bOk=true;
			}
		}
		return $bOk;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	/*
	 * $p_type=0	: unspecified or dynamic
	 * $p_type=1	: only insert operation
	 * $p_type=2	: only update operation
	 */
	function sqlInsertUpdate($p_fields, $p_pk_value=NULL, $p_pk_name=NULL, $p_table=NULL, $p_pk_remove=true, $p_type=0){
		$f_return=false;
		$f_update=false;
		if(strlen($this->curtable)>0) $f_table=$this->curtable;
		if(strlen($p_table)>0) $f_table=$p_table;
		if(isset($p_pk_value)) $f_pk_value=$p_pk_value;
		if(strlen($p_pk_name)>0){
			if(isset($p_pk_value)) $f_update=$this->isValExist($p_pk_value, $p_pk_name, $f_table);
			if(true==$p_pk_remove) unset($p_fields[$p_pk_name]);
			elseif(isset($p_fields[$p_pk_name])) $f_pk_value=$p_fields[$p_pk_name];
		}
		$v_sql=$this->getInsSql($p_fields, $f_table);
		if($p_type==0 || $p_type==2){
			if(true==$f_update) $v_sql=$this->getUpdtSql($p_fields, '`'.$p_pk_name.'`=\''.$p_pk_value.'\'', $f_table);
			if($p_type==2 && strlen($p_pk_name)<=0) $v_sql='';
			if($p_type==2 && false==$f_update) $v_sql='';
		}
		if(strlen($v_sql)>0){
			$this->lastSQLquery=$v_sql;
			$v_rs=$this->runQuery($v_sql);
			if($v_rs){
				$this->lastSuccessfulSql=$v_sql;
				if(isset($f_pk_value)) $f_return=$f_pk_value;
				if(false==$f_update){
					if(!isset($p_pk_value)){
						if(is_resource($this->curlink)) $f_return=@mysql_insert_id($this->curlink);
						else $f_return=@mysql_insert_id();
					}
					$this->lastInsertedID=$f_return;
				}
				$this->curtable=trim($f_table);
			}
		}
		return $f_return;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getTotalRows($p_rs=NULL){
		$r_rows=0;
		if(is_resource($p_rs)) $r_rows=mysql_num_rows($p_rs);
		return $r_rows;
	}

/*
	usage				: total number of rows in value useing sql count() function
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20071206
	last modified date	: 20071206
*/
	function getTotalRowsX($p_table_name, $p_condition=NULL){
		$f_dbname=(strlen($this->curdatabase)>0?'`'.$this->curdatabase.'`.':'');
		$f_sql='select count(*) from '.$f_dbname.'`'.$p_table_name.'`';
		if(NULL!=$p_condition) $f_sql.=' where '.$p_condition;
		$f_tot=0;
		$f_get=$this->getRowsVal($f_sql, 0);
		if(intval($f_get)>0) $f_tot=intval($f_get);
		return $f_tot;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20091126
*/
	function getInsSql($p_fields, $p_table=NULL){
		$return_sql='';
		if(strlen($p_table)>0) $f_table=$p_table;
		elseif(strlen($this->curtable)>0) $f_table=$this->curtable;
		if(isset($f_table) && is_array($p_fields)){
			$sap=NULL;
			while (list($key, $val) = each($p_fields)){
				if(strlen(trim($val))>0){
					$keys_ins.=$sap."`$key`";
					$vals_ins.=$sap."'".$this->sqlSafeString($val)."'";
					$sap=', ';
				}
			}
			$f_dbname=(strlen($this->curdatabase)>0?'`'.$this->curdatabase.'`.':'');
			$return_sql="insert into $f_dbname`$f_table` ( $keys_ins ) values ( $vals_ins )";
		}
		return $return_sql;
	}
	
/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20091126
*/
	function getUpdtSql($p_fields, $p_condition, $p_table=''){
		$return_sql='';
		if(strlen($p_table)>0) $f_table=$p_table;
		elseif(strlen($this->v_table)>0) $f_table=$this->v_table;
		if(isset($f_table) && is_array($p_fields)){
			$sap = "";
			while (list($key, $val)=each($p_fields)){
				$set_fields.=$sap.' `'.$key.'`='.(strlen(trim($val))>0?"'".$this->sqlSafeString($val)."'":'NULL');
				$sap = ', ';
			}
			$f_dbname=(strlen($this->curdatabase)>0?'`'.$this->curdatabase.'`.':'');
			$return_sql="update $f_dbname`$p_table` set $set_fields where $p_condition";
		}
		return $return_sql;
	}
	
/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getSqlForLikeOperator($p_fld, $p_val, $p_condition='and', $p_condsap=','){
		$return_val=NULL;
		if(strlen($p_fld)>0 && strlen($p_val)>0){
			$vals=explode($p_condsap, trim($p_val));
			$frstTm=true;
			if(is_array($vals) && count($vals)>0){
				foreach($vals as $valX){
					if($frstTm==false) $return_valX.=" ".$p_condition." ";
					$val=strtolower(trim($valX));
					$return_valX.="lower(`".trim($p_fld)."`) like '%".$this->sqlSafeString($val)."%'";
					$frstTm=false;
				}
				$return_val=' ('.$return_valX.') ';
			}
		}
		return $return_val;
	}
	
/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getRowsResult($p_sql, $p_row_idx=0, $fld=NULL){
		$fVal=false;
		$this->lastSQLquery=$p_sql;
		$f_rs=$this->runQuery($p_sql);
		if($f_rs){
			$this->lastSuccessfulSql=$p_sql;
			if($this->getTotalRows($f_rs)>0){
				if(isset($fld)) $fVal=mysql_result($f_rs, intval($p_row_idx), $fld);
				else $fVal=mysql_result($f_rs, intval($p_row_idx));
			}
		}
		return $fVal;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getRowsVal($p_sql, $p_col_idx=-1){
		$this->lastSQLquery=$p_sql;
		$f_rs=$this->runQuery($p_sql);
		if($f_rs){
			$this->lastSuccessfulSql=$p_sql;
			if($this->getTotalRows($f_rs)>0){
				$f_rw=$this->sqlFetchRow($f_rs);
				if(intval($p_col_idx)>=0) return stripslashes($f_rw[intval($p_col_idx)]);
				else return $f_rw;
			}else return true;
		}else return false;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20080402
	last modified date	: 20080402
	
	usages				: return array fetch with assoc mode
*/
	function getRowsValX($p_sql, $p_rwnum=0){
		$f_rtrn=false;
		$this->lastSQLquery=$p_sql;
		$f_rs=$this->runQuery($p_sql);
		if($f_rs){
			$this->lastSuccessfulSql=$p_sql;
			if($this->getTotalRows($f_rs)>0){
				$f_ctr=0;
				$f_fl=true;
				while(true==$f_fl){
					$f_rw=$this->sqlFetchAssoc($f_rs);
					if($f_ctr==$p_rwnum && true==$f_fl){
						$f_fl=false;
						unset($f_rtrn);
						foreach($f_rw as $fld=>$val) $f_rtrn[$fld]=stripslashes($val);
					}
					$f_ctr++;
				}
			}
		}
		return $f_rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	static function getMysqlDtTm($p_dt=0){
//		if(!isset($this->clsCore)) trigger_error('You must set core class instance. Use setCoreClassInstance($pi_core_class) to set.', E_USER_ERROR);
		if($p_dt<=0) $p_dt=(isset(sqlFunctions::$curSystemTime)?sqlFunctions::$curSystemTime:time());
		return date("Y-m-d H:i:s", $p_dt);
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20091126
	
	usage: isValExist($kwrd, 'keyword', 'resume_keywords', true, "and `skcid`='$skcid'")
*/
	function isValExist($p_value, $p_field_name='id', $p_table=NULL, $p_case_ignored = true, $p_extra_condition = NULL){
		$return_val = false;
		if(strlen($p_table)>0) $p_table_name=$p_table;
		elseif(strlen($this->v_table)>0) $p_table_name=$this->v_table;
		if(strlen(trim($p_value))>0 && isset($p_table_name)>0){
			$v_value = trim($p_value);
			$condition1 = "lower(`".$p_field_name."`)='".strtolower($this->sqlSafeString($v_value))."'";
			if($p_case_ignored == false) $condition1="`".trim($p_field_name)."` = '".$this->sqlSafeString($v_value)."'";
			$f_dbname=(strlen($this->curdatabase)>0?'`'.$this->curdatabase.'`.':'');
			$v_sql = "select `".trim($p_field_name)."` from $f_dbname`".trim($p_table_name)."` where $condition1";
			if(strlen($p_extra_condition) > 0) $v_sql.=' '.trim($p_extra_condition);
			$v_rs = $this->runQuery($v_sql);
			if($v_rs){
				if($this->getTotalRows($v_rs)>0){
					$v_rw=$this->sqlFetchRow($v_rs);
					$v_data = trim($v_rw[0]);
					if($v_data == $v_value) $return_val = true;
				}
			}
		}
		return $return_val;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20090616
	last modified date	: 20090616
*/
	function getFetchedSafeRowArray($p_rw=NULL){
		if(true==$this->ensureSafeData && is_array($p_rw) && isset($p_rw)){
			foreach($p_rw as $k=>$v) $f_rtrn[$k]=(is_null($v)?NULL:stripslashes($v));
		}else $f_rtrn=$p_rw;
		return $f_rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function sqlFetchAssoc($p_rs=NULL){
		$frs=$this->myrs;
		if(isset($p_rs)) $frs=$p_rs;
		if($frs){
			$r_rw=mysql_fetch_assoc($frs);
			if(!isset($p_rs)) $this->myrs=$frs;
			$rtrn=(true==$this->ensureSafeData?$this->getFetchedSafeRowArray($r_rw):$r_rw);
		}
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20080522
*/
	function sqlFetchRow($p_rs=NULL){
		$frs=$this->myrs;
		if(isset($p_rs)) $frs=$p_rs;
		if($frs){
			$r_rw=mysql_fetch_row($frs);
			if(!isset($p_rs)) $this->myrs=$frs;
			$rtrn=(true==$this->ensureSafeData?$this->getFetchedSafeRowArray($r_rw):$r_rw);
		}
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function sqlFetchArray($p_rs=NULL){
		$frs=$this->myrs;
		if(isset($p_rs)) $frs=$p_rs;
		if($frs){
			$r_rw=mysql_fetch_array($frs);
			if(!isset($p_rs)) $this->myrs=$frs;
			$rtrn=(true==$this->ensureSafeData?$this->getFetchedSafeRowArray($r_rw):$r_rw);
		}
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20080522
*/
	function sqlFetchObject($p_rs=NULL, $p_result_type=NULL){
		$frs=$this->myrs;
		if(isset($p_rs)) $frs=$p_rs;
		if($frs){
			$r_rw=mysql_fetch_object($frs, $p_result_type);
			if(!isset($p_rs)) $this->myrs=$frs;
			$rtrn=(true==$this->ensureSafeData?$this->getFetchedSafeRowArray($r_rw):$r_rw);
		}
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 
	last modified date	: 
	function sqlGetResult($p_rs=NULL, $p_rownum=0){
		$frs=$this->myrs;
		if(isset($p_rs)) $frs=$p_rs;
		if($frs){
			$r_rw=mysql_result($frs, $p_rownum, );
			if(!isset($p_rs)) $this->myrs=$frs;
		}
		if(is_array($r_rw) && isset($r_rw)){
			foreach($r_rw as $k=>$v) $f_rtrn[$k]=stripslashes($v);
		}
		return $f_rtrn;
	}
*/

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getLastExecutedQuery(){
		return $this->lastSQLquery;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function getLastSuccessfulQuery(){
		return $this->lastSuccessfulSql;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20080522
*/
	function getLastInsertedID($p_lnk=NULL){
		if(isset($this->curlink) && is_resource($this->curlink)) $flnkid=$this->curlink;
		if(isset($p_lnk) && is_resource($p_lnk)) $flnkid=$p_lnk;
		if(!isset($this->lastInsertedID)){
			if(isset($flnkid)) $this->lastInsertedID=mysql_insert_id($flnkid);
			else $this->lastInsertedID=mysql_insert_id();
		}
		$rtrn=$this->lastInsertedID;
		unset($this->lastInsertedID);
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20080522
*/
	function runQuery($p_sql, $p_lnk_identifier=NULL){
		$this->lastAffectedRows=0; // this is not implemented very well.. i plan to implement this function later...
		$this->lastSQLquery=$p_sql;
		$flnkid=(is_resource($p_lnk_identifier)?$p_lnk_identifier:(is_resource($this->curlink)?$this->curlink:false));
		$rs=(is_resource($flnkid)?mysql_query($p_sql, $flnkid):mysql_query($p_sql));
		if($rs){
			$this->lstSRWCLID=(isset($p_lnk_identifier)?false:true);
			$this->lastAffectedRows=(is_resource($flnkid)?mysql_affected_rows($flnkid):mysql_affected_rows()); // this is not implemented very well.. i plan to implement this function later... so please use this at your own risk!!!
			$this->lastSuccessfulSql=$p_sql;
			$rtrn=$this->myrs=$rs;
			if(true==$this->updtFlag && $this->lastAffectedRows<=0) $rtrn=false;
			$this->updtFlag=false;
		}else{
			echo("<div><strong>SQL Syntex Error...</strong><br><em>$p_sql</em></div>");
			trigger_error("<div><font color=red>".(is_resource($flnkid)?mysql_error($flnkid):mysql_error())."</font></div>", E_USER_ERROR);
		}
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20100610
	last modified date	: 20100610
*/
	function isMysqlLinkAlive($p_lnk_identifier=NULL){
		$flnkid=(is_resource($p_lnk_identifier)?$p_lnk_identifier:(is_resource($this->curlink)?$this->curlink:false));
		$rtrn=(is_resource($flnkid)?mysql_ping($flnkid):mysql_ping());
		return $rtrn;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20071031
	last modified date	: 20080522
*/
	function getAllRows($p_sql, $p_lnk_identifier=NULL){
		$cf_return=false;
		$rs=$this->runQuery($p_sql, $p_lnk_identifier);
		if($rs && $this->getTotalRows($rs)>0){
			$ctr=0;
			while($rw=$this->sqlFetchAssoc($rs)){
				foreach($rw as $k=>$v) $rx[$k]=stripslashes($v);
				$cf_return[$ctr]=$rx;
				$ctr++;
			}
		}
		return $cf_return;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20071030
	last modified date	: 20080522
*/
	function getRows($p_idfieldname, $p_sql, $p_lnk_identifier=NULL){
		$cf_return=false;
		$rs=$this->runQuery($p_sql, $p_lnk_identifier);
		if($rs){
			if($this->getTotalRows($rs)>0){
				while($rw=$this->sqlFetchAssoc($rs)){
					foreach($rw as $k=>$v) $rx[$k]=stripslashes($v);
					$cf_return[$rx[$p_idfieldname]]=$rx;
				}
			}
		}
		return $cf_return;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20071031
	last modified date	: 20080522
*/
	function getArrWithIdAsKey($p_sql, $p_lnk_identifier=NULL){
		$cf_return=false;
		$rs=$this->runQuery($p_sql, $p_lnk_identifier);
		if($rs){
			if($this->getTotalRows($rs)>0){
				while($rw=$this->sqlFetchRow($rs)) $cf_return[trim($rw[0])]=trim(stripslashes($rw[1]));
			}
		}
		return $cf_return;
	}

/*
	real escaping or addslashes after checking magic quotes enable or not...
	for security measure of sql injection or sql hijeking..
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20080527
	last modified date	: 20080527
*/
	static function sqlSafeString($p_val, $p_real_escaping_enabled=true) {
		if(true==$p_real_escaping_enabled) $f_val=mysql_real_escape_string((get_magic_quotes_gpc()==yes?stripslashes($p_val):$p_val));
		else $f_val=(get_magic_quotes_gpc()==yes?$p_val:addslashes($p_val));
		return $f_val;
	}


/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function isValidDateTime($dateTime){
		if(strlen($dateTime)>=19 && preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)){
			if(checkdate($matches[2], $matches[3], $matches[1])) return true;
		}
		return false;
	}

/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20101027
	last modified date	: 20101027
*/
	function delFromSingleTable($p_table, $p_cond_arr=NULL, $p_common_cond=NULL){
		$del_sql='delete from `'.$p_table.'`';
		if((isset($p_cond_arr) && is_array($p_cond_arr)) || isset($p_common_cond)){
			if(isset($p_cond_arr) && is_array($p_cond_arr)){
				if(isset($p_common_cond)) $p_common_cond_sql=' AND '.$p_common_cond;
				foreach($p_cond_arr as $idx=>$val){
					$opt=false;
					$opt_num=false;
					foreach($val as $idx2=>$val2){
						if(is_numeric($idx2) && false===$opt){
							$cond_sqlX.=$idx2_num_sap.' \''.$val2.'\'';
							$idx2_num_sap=', ';
							$opt_num=true;
						}elseif(false===$opt_num && !is_numeric($idx2)){
							$cond_sqlX.=$idx1_sap.' `'.$idx2.'`=\''.$val2.'\' ';
							$idx1_sap=' AND ';
							$opt=true;
						}
					}
					if(isset($cond_sql)){
						if(true===$opt) $cond_sql.=$idx_sap.' ('.$cond_sqlX.$p_common_cond_sql.') ';
						if(true==$opt_num) $cond_sql.=$idx_sap.' (`'.$idx.'` IN ('.$cond_sqlX.') '.$p_common_cond_sql.') ';
						if(true===$opt || true==$opt_num) $idx_sap=' OR ';
						unset($cond_sqlX);
					}
					if(!isset($cond_sql)) $cond_sql='1';
					$del_sql.=' where '.$cond_sql;
				}
			}else $del_sql.=' where '.$p_common_cond;
		}else $del_sql.=' where 1';
		return $this->runQuery($del_sql);
	}


/*
	
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20091011
	last modified date	: 20091011
*/
	function isDataNeedUpdate($p_source_sql, $p_comparing_array, $p_case_ignore=true) {
		$rtrn=false;
		if(is_array($p_comparing_array)){
			$rtrn=(count($p_comparing_array)>0?true:false);
			$source=$this->getAllRows($p_source_sql);
			if(is_array($source) && count($source)>0){
				$rtrn=true;
				if(count($source)==count($p_comparing_array)){
					$rtrn=false;
					foreach($source as $rw){
						$data=reset($rw);
						$found=false;
						foreach($p_comparing_array as $current){
							$found=((true==$p_case_ignore && strtolower($current)==strtolower($data)) || (false==$p_case_ignore && $current==$data))?true:false;
							if(true==$found) break;
						}
						if(false==$found){
							$rtrn=true;
							break;
						}
					}
				} //if(count($source)==count($p_comparing_array)){
			} //if(is_array($source) && count($source)>0){
		}
		return $rtrn;
	}


/*
	created by			: Md. Shahadat Hossain Khan Razon (razonklnbd@yahoo.com)
	created date		: 20060517
	last modified date	: 20071030
*/
	function __destruct(){
		if(is_resource($this->curlink)) mysql_close($this->curlink);
		if(isset($this->curlink)) unset($this->curlink);
		unset($this->curdatabase);
		$this->curtable='';
		$this->lastSQLquery='';
		$this->lastSuccessfulSql='';
	}

}