Location: PHPKode > scripts > dbObject > dbobject/dbObject.inc
<?

class dbObject  {

	var $hostname = "";
	var $database = "";
	var $username = "";
	var $password = "";
	var $conLink = false;
	var $conLinkID;
	var $result = false;
	var $errorMsg = false;
	var $resultArray = false;
	var $curRow = false;
	var $resultCount = false;
	var $queryString;
	var $queryExecTime;
	var $queryStoreTime;
	var $queryTime;
	var $doLog = true;
	var $logfile = "c:\php\dbObject.log";
	var $insert_id;
	var $affected_rows;
	var $debug = false;
	var $queryid = 0;
	
	function dbObject($hostname="",$database="",$username="",$password="") {
		$this->conLinkID = rand();
		if($hostname)
			$this->hostname = $hostname;
		if($database)
			$this->database = $database;
		if($username)			
			$this->username = $username;		
		if($password)			
			$this->password = $password;
		$this->logger("New instance of supClass::Db created");
		return true;
	}
	
		function connect() {
		if(@!$this->conLink = mysql_connect("$this->hostname","$this->username","$this->password")) {
			$this->logger("Could not connect: ".$this->error());
			return false;
		}
		if($this->database)
			$this->selectDb($this->database);
		$this->logger("Connected to '$this->hostname' [$this->conLink]");
		return $this->conLink;
	}
	
	function isConnected() {
		if(is_resource($this->conLink))
			return true;
		else
			return false;
	}
	
	function selectDb ($db) {
		$this->database = $db;
		if(!$this->conLink) {
			$this->logger("Could not select database '$this->database': ".$this->error());
			return false;
		}
		if(!mysql_select_db($this->database,$this->conLink)) {
			$this->logger("Could not select database '$this->database': ".$this->error());
			return false;
		}
		$this->logger("Selected '$this->database'");
		return true;
	}
	
	function close() {
		if(@!mysql_close($this->conLink)) {
			$this->logger("Connection closed failed: no open connection");
			return false;
		}
		$this->logger("Connection closed: [$this->conLink]");
		return true;
	}
	
	function logger($string) {
		if($this->debug)
			echo date('Y-m-d H:i:s')."	[$this->conLinkID]	From: ".getenv("REMOTE_ADDR")." [$this->username]	$string".chr(13).chr(10)."<br>";
		if(!string || !$this->logfile || !$this->doLog)
			return false;
		$fp = fopen($this->logfile,"a");
		$write = fputs($fp,date('Y-m-d H:i:s')."	[$this->conLinkID]	From: ".getenv("REMOTE_ADDR")." [$this->username]	$string".chr(13).chr(10));
		fclose($fp);
		return true;
	}
	
	function error() {
		@$this->errorMsg = mysql_error();
		return $this->errorMsg;
	}
		
	function getmicrotime(){ 
		list($usec, $sec) = explode(" ",microtime()); 
		return ((float)$usec + (float)$sec); 
    } 
	
	function getTotalExecTime() {
		while(list($key, $val) = each($this->queryTime))
			$tot = $tot+$val;
		return $tot;
	}
	
	function query($str,$store=true,$insert=false) {
		$this->queryid++;
		$this->queryString[$this->queryid] = $str;
		if(!$this->conLink) {
			$this->logger("Query failed: '".$this->queryString[$this->queryid]."' ".$this->error());
			return false;
		}
		$start = $this->getmicrotime();
		if(!$this->result[$this->queryid] = mysql_query($this->queryString[$this->queryid])) {
			$this->logger("Query failed: '".$this->queryString[$this->queryid]."' ".$this->error());
			return false;
		}
		if($insert)
			$store = false;
		if(!$store) {
			if($insert) 
				$this->insert_id[$this->queryid] = mysql_insert_id($this->conLink);
			$end = $this->getmicrotime();
			$this->queryExecTime[$this->queryid] = round($end-$start,4);
			$this->logger("Query executed: '".$this->queryString[$this->queryid]."' in ".$this->queryExecTime[$this->queryid]." seconds");
			return $this->queryid;
		}
		$i = 0;
		$storetime = $this->getmicrotime();
		while($row = mysql_fetch_assoc($this->result[$this->queryid])) {
			$keys = $this->buildKeys($row);
			$this->rarray[$i] = $keys;
			$i++;
		}
		$this->resultArray[$this->queryid] = $this->rarray;;
		$storetimee = $this->getmicrotime();
		$this->queryStoreTime[$this->queryid] = round($storetimee-$storetime,4);
		$this->logger("Stored resultset and built keys on $i rows in ".$this->queryStoreTime[$this->queryid]." seconds");
		mysql_free_result($this->result[$this->queryid]);
		$this->resultCount[$this->queryid] = $i;
		$this->curRow[$this->queryid] = -1;
		$end = $this->getmicrotime();
		$this->queryExecTime[$this->queryid] = round($end-$start,4);
		$this->queryTime[$this->queryid] = $this->queryExecTime[$this->queryid]+$this->queryStoreTime[$this->queryid];
		$this->logger("Query executed: '".$this->queryString[$this->queryid]."' in ".$this->queryExecTime[$this->queryid]." seconds");
		$this->logger("Total querytime: ".$this->queryTime[$this->queryid]." seconds");
		return $this->queryid;
	}
	
	function buildKeys($row) {
		$i = 0;		
		$keys = array();
		while(list($key,$val) = each($row)) {
			$keys[$key] = $val;
			$keys[$i] = $val;
			$i++;
		}
		return $keys;
	}
	
	function insert($inserts,$table) {
		if(!$inserts || !$table)
			return false;
		if(!is_array($inserts)) {
			$this->logger("INSERT: parameter was not an array.");
			return false;
		}
		while(list($key,$val) = each($inserts)) {
			$values = $values . "'$val',";
			$cols = $cols . "$key,";
		}
		$str = "insert into $table(".substr($cols,0,strlen($cols)-1).") values(".substr($values,0,strlen($values)-1).")";
		return $this->query($str,0,1);
	}
	
	function del($string) {
		$this->queryid++;
		$start = $this->getmicrotime();
		if(!$string)
			return false;
		$this->queryString[$this->queryid] = $string;
		if(!$result = mysql_query($this->queryString[$this->queryid])) {
			$this->logger("DEL: error while executing query '".$this->queryString[$this->queryid]."'".chr(13).chr(10).$this->error());
			return false;
		}
		$this->affected_rows = mysql_affected_rows($this->conLink);
		$end = $this->getmicrotime();
		$this->queryExecTime[$this->queryid] = round($end-$start,4);
		$this->logger("Delete query executed: '".$this->queryString[$this->queryid]."' in ".$this->queryExecTime[$this->queryid]." seconds");
		return true;
	}
	
	function upd($fields=false,$table=false,$where=false,$string=false) {
		$this->queryid++;
		if(!is_array($fields) && !$table && !$where && !$string)
			return false;
		$start = $this->getmicrotime();
		if(is_array($fields) && $table) {
			while(list($key,$val) = each($fields)) {
				$values = $values . "$key='$val',";
			}
			if($where) 
				$this->queryString[$this->queryid] = "update $table set ".substr($values,0,strlen($values)-1) ." where $where";
			else
				$this->queryString[$this->queryid] = "update $table set ".substr($values,0,strlen($values)-1);
		} else {
			if($string)
				$this->queryString[$this->queryid] = $string;
			else
				return false;
		}
		if(!$result = mysql_unbuffered_query($this->queryString[$this->queryid])) {
			$this->logger("UPD: error while executing query '".$this->queryString[$this->queryid]."'".chr(13).chr(10).$this->error());
			return false;
		} else {
			$this->affected_rows[$this->queryid] = mysql_affected_rows($this->conLink);
			$end = $this->getmicrotime();
			$this->queryExecTime[$this->queryid] = round($end-$start,4);
			$this->logger("Update query executed: '".$this->queryString[$this->queryid]."' in ".$this->queryExecTime[$this->queryid]." seconds");
			return true;
		}
	}
	
	function first($format="",$queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$fields = $resultset[0];
		if(!$fields) {
			$this->logger("FIRST: could not get first row in resultset.");
			return false;
			}
		$this->curRow = 0;
		if(!$format)
			return $fields;
		$formatTmp = $format;
		while( list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}
	
	function curr($format="",$queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$fields = $resultset[$this->curRow[queryid]];
		if(!$fields) {
			$this->logger("CURRENT: could not get current row in resultset.");
		}
		if(!$format)
			return $fields;
		$formatTmp = $format;
		while( list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}

	function last($format="",$queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$fields = $this->resultset[$this->resultCount[queryid]-1];
		if(!$fields) {
			$this->logger("LAST: could not get last row in resultset.");
			return false;
		}
		$this->curRow[$queryid] = $this->resultCount[$queryid];
		if(!$format)
			return $fields;
		$formatTmp = $format;
		while( list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}
	
	function getRow($format="",$queryid=false,$curRow=-1) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		if($curRow == -1)
			$curRow = $this->curRow[$queryid];
		$fields = $resultset[$curRow];
		if(!$fields) {
			$this->logger("ROW: could not get row ".$curRow[$this->queryid]." in resultset.");
			return false;
		}
		if(!$format)
			return $fields;
		$formatTmp = $format;
		while( list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}
	
	function next($format="",$queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$this->curRow[$queryid]++;
		if($this->resultCount[$queryid] <= $this->curRow[$queryid])
			return false;
		$fields = $resultset[$this->curRow[$queryid]];
		if(!$fields) {
			$this->logger("NEXT: could not get next row in resultset (".$this->curRow[$this->queryid]." / ".$this->resultCount[$this->queryid].")");
			return false;
		} 
		if(!$format) 
			return true; 
		$formatTmp = $format;
		while(list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}
  
	function prev($format="",$queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$this->curRow[$queryid]--;
		if($this->curRow[$queryid] < 0)
			return false;
		$fields = $resultset[$this->curRow[$queryid]];
		if(!$fields) {
			$this->logger("NEXT: could not get prev row in resultset (".$this->curRow[$this->queryid]." / ".$this->resultCount[$this->queryid].")");
			return false;
		} 
		if(!$format)
			return true;
		$formatTmp = $format;
		while( list($key,$val) = each($fields) ) {
			$formatTmp = str_replace("<!--$key-->",$val,$formatTmp);
		}
		return $formatTmp;
	}

	function getVal($fieldname,$queryid=false,$curRow=-1) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		if($curRow == -1)
			$curRow = $this->curRow[$queryid];
		$row = $resultset[$curRow];
		$value = $row[$fieldname];
		if(!$value) {
			$this->logger("GETVAL: Could not get requested field '$fieldname'");
			return false;
		}
		return $value;
	}
	
	function showResult($queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$resultset = $this->resultArray[$queryid];
		$string = "<table border=0>";
		while(list($rowkey,$row) = each($resultset)) {
			$string = $string . "<tr><td width=40><b>row:</b></td><td colspan=2>$rowkey</td></tr>\n";
			while(list($key,$val) = each($row)) {
			if($bg == "#ffffff") { $bg = "#f0f0f0"; } else { $bg = "#ffffff"; }
				$string = $string . "<tr><td>&nbsp;</td>
				<td bgcolor=\"$bg\" width=60><b>name:</b></td>
				<td bgcolor=\"$bg\">$key</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td bgcolor=\"$bg\"><b>value:</b></td>
				<td bgcolor=\"$bg\">$val</td>
			</tr>\n";
			}
		}
		$string = $string . "</table>";
		return $string;
	}
	
	function resetResult($queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$this->curRow[$queryid] = -1;
		$this->logger("Reseted resultset.");
		return true;
	}
	
	function clearResult($queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		$this->resultArray[$queryid] = false;
		$this->curRow[$queryid] = -1;
		$this->resultCount[$queryid] = 0;
		$this->logger("Resultset cleared");
		return true;
	}
	
	function reverseResult($queryid=false) {
		if(!$queryid)
			$queryid = $this->queryid;
		if(!rsort($this->resultArray[$queryid])) {
			$this->logger("Resultset reverse failed");
			return false;
		}
		$this->logger("Resultset reversed");
		return true;
	}

	function clearAll () {
		$this->resultArray = false;
		$this->curRow = -1;
		$this->resultCount = 0;
		$this->logger("All resultsets cleared");
	return true;
	}
	
}
?>
Return current item: dbObject