Location: PHPKode > scripts > Simple db class > simple-db-class/class.database.php
<?php
class database{
	private $hConn;
	public $showQueries;
/*database constructor start*/
	public function __construct(){
		global $cfg;
	
		$cfg['db']['host']			= "localhost";
		$cfg['db']['user']			= "bkrishan";
		$cfg['db']['password']		= "bk123";
		$cfg['db']['name']			= "test_bk";
		$cfg['db']['showQueries']	= false;
	//	$cfg['db']['showQueries']	= true;
	
		$this->showQueries = $cfg['db']['showQueries'];
		
		$this->hConn = mysql_connect($cfg['db']['host'],$cfg['db']['user'],$cfg['db']['password']) or die(mysql_error());

		if(!is_resource($this->hConn)){
			throw new Exception("Unable to connect to database using \"$connString\"",E_USER_ERROR);
		}else{
			//echo "connected";
		}

		$this->selectDb($cfg['db']['name']);
	}

/*database constructor end*/	
/*select database start*/	

	private function selectDb($dbName){
		if(!mysql_select_db($dbName)){
			throw new Exception("Unable to select database",E_USER_ERROR);	
		}else{
			//echo "database selected";
		}
	}
/*select database end*/

	public function __destruct(){
		if(is_resource($this->hConn)){
			mysql_close($this->hConn);
		}
	}

/*select function  start*/
	public function select($sql){
		if($this->showQueries){
			$this->showQueries($sql);
		}
		$hRes = mysql_query($sql,$this->hConn);
		if(!is_resource($hRes)){
			$err = mysql_error($this->hConn);
				throw new exception($err);
		}
		$arReturn = array();
		while(($row =mysql_fetch_assoc($hRes))){
			$arReturn[] = $row;	
		}
		return $arReturn;
	}

/*select function  end*/
/*insert  function  start*/

	public function insert($table,$arFieldValues){
		$fields		= array_keys($arFieldValues);
		$values		= array_values($arFieldValues);
		$escVals	= array();
		foreach($values as $val){
			if(!is_numeric($val)){
				$val = "'".mysql_escape_string($val)."'";
			}
			$escVals[] = $val;	
		}
		$sql = "INSERT INTO $table (";
		$sql .=join(', ', $fields);
		$sql .=') VALUES(';
		$sql .=join(', ', $escVals);
		$sql .=')';

		if($this->showQueries){
			$this->showQueries($sql);
		}
		$hRes = mysql_query($sql,$this->hConn);
		if(!$hRes){
			$err = mysql_error($this->hConn)."\n".$sql;
			throw new exception($err);
		}
		//return mysql_affected_rows();
		return mysql_insert_id();
	}

/*insert  function  end*/
/*update  function  starts*/
	
	public function update($table,$arFieldValues,$arConditions){
		$arUpdates = array();
		foreach($arFieldValues as $field => $val){
			if(! is_numeric($val)){
				$val = "'". mysql_escape_string($val). "'";
			}
			$arUpdates[] = "$field = $val";
		}

		$arWhere = array();
		foreach($arConditions as $field => $val){
			if(!is_numeric($val)){
				$val = "'". mysql_escape_string($val) ."'"; 
			}
			$arWhere[] = "$field = $val";
		}

		$sql = "UPDATE $table SET ";
		$sql .= join(', ', $arUpdates);
		$sql .= ' WHERE '. join(' AND ', $arWhere);

		if($this->showQueries){
			$this->showQueries($sql);
		}
		$hRes = mysql_query($sql);
		if(!$hRes){
			$err = mysql_error($this->hConn).$sql;
			throw new Exception($err);
		}
		//return mysql_affected_rows();
		return mysql_affected_rows();
	}
/*update  function  starts*/
/*delete  function  starts*/
	
	public function delete($table,$arConditions){
		$arWhere = array();
		
		foreach($arConditions as $field => $val){
			if(!is_numeric($val)){
				$val = "'". mysql_escape_string($val) ."'"; 
			}
			$arWhere[] = "$field = $val";
		}
		
		$sql = "DELETE FROM $table WHERE ". join(' AND ', $arWhere);

		if($this->showQueries){
			$this->showQueries($sql);
		}

		$hRes = mysql_query($sql);
		if(!$hRes){
			$err = mysql_error($this->hConn).$sql;
			throw new Exception($err);
		}
		return mysql_affected_rows();
	}

	public function password($password){
			return $password = md5($password);
		}
	
	public function makeSelectOptions($arrData,$name,$selectedIndex=0){
		$strOptions= "";
		if(is_array($arrData) && count($arrData)>0){
			$strOptions = "<select name ='".$name."'>";	
			$strOptions .= "<option value =''>-select-</option>";	
			foreach($arrData as $k=>$v){
				$selected= "";
				if($selectedIndex == $k){
					$selected= "selected";
				}
				$strOptions .= "<option value ='".$k."' ".$selected.">".$v."</option>";	
			}
			$strOptions .= "</select>";	
		}
		return $strOptions;
	}

/*delete  function  end*/
/*display  queries function  starts*/

	public function showQueries($sql){
		echo "<font style ='color:#ff0000'>".$sql."<br/></font>";
	}
/*display  queries function  end*/
}
//$objDatabase = new database();
?>
Return current item: Simple db class