<?php
if(! defined('ON')){ die(); } 
/* 
	|||||||||||||||||||||||||||||||||||||||||
	|  Writed by Fahad Salem Al-Ghudaier	|
	|  www.phpx3.com | faha1ad2@hotmail.com	|
	|  Simple Database Handle Class			|
	|||||||||||||||||||||||||||||||||||||||||
	
	#query 		: $db->query
	#count 		: $db->count
	#fetch 		: $db->fetch
	#insert 	: $db->insert(table,array(col,col),array(value,value))
	#uquery 	: $db->id_query("SELECT * FROM `table` WHERE `id`='1'"); <<== automatic fetch and everything
	#num		: $db->rows_num;
	#insertid	: $db->insert_id()
	#update_h	: $db->update_hits(table,hits,15)
	#close		: $db->close
	#mudelete	: $db->multi_delete
	#update		: $db->update('table',array(text,subject),array($text,$subject),5,id_name)
	#id_exist	: $db->id_exist(table,5,id);
	#tables		: $this->create_table($table,$columns,$type,$extra='',$default=''$primary='id')
*/
class db{
	var $Querys	= 0;
	
	/*public function errors($choose){
		if($choose == false){
			error_reporting(0);
		}
	}*/
	
	
	public function connect($server,$user,$pass){
		if(mysql_connect($server,$user,$pass)){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	public function select_db($name){
		if(mysql_select_db($name)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public function query($query){
		$this->Querys++;
		$query	= mysql_query($query);
		if($query){
			return $query;
		}else{
			return false;
		}
	}
	
	

	public function id_query($query){
		if($this->rows_num($this->query($query)) == 0){
			return false;
		}else{
			return $this->fetch($this->query($query));
		}
	}
	
	
	
	public function fetch($query){
		return mysql_fetch_array($query);
	}
	
	
	public function rows_num($query){
		return mysql_num_rows($query);
	}
	
	
	public function count($table,$CONDITIONS='',$id='id'){
		if(! empty($CONDITIONS)){
			$CONDITIONS = "WHERE".$CONDITIONS;
		}								
		$number	= $this->fetch($this->query("SELECT COUNT(`".$id."`) AS num FROM `".$table."` ".$CONDITIONS.""));
		return $number['num'];
	}
	
	
	
	public function insert_id(){
		return mysql_insert_id();
	}
	
	
	
	public function insert($table,$columns,$values){
		$ivar	= "INSERT INTO `".$table."` (";		
		$i	= 0;
		$count	 = count($columns);
		foreach($columns as $value){
			$i++;
			$ivar	.= "`".$value."`";
			if($i < $count){ $ivar	.= ','; }
		}
		
		$ivar	.= ') VALUES (';
				
		$i	= 0;
		$count	 = count($values);
		foreach($values as $value){
			$i++;
			$ivar	.= "'".$value."'";
			if($i < $count){ $ivar	.= ','; }
		}
		
		$ivar	.= ')';
		return $this->query($ivar);
	}
	
	
	
	
	public function update($table,$columns,$values,$id,$id2='id'){
		$ivar	= "UPDATE `".$table."` SET";		
		$i	= 0;
		$i2	= 0;
		$count	 = count($columns);
		foreach($columns as $value){
			$i2++;
			$ivar	.= "`".$value."`='".$values[$i]."'";
			if($i2 < $count){ $ivar	.= ','; }
			$i++;
		}
		$ivar	.= "WHERE `".$id2."`='".$id."'";
		//echo $ivar;
		return $this->query($ivar);
	}
	
	
	
	
	
	
	public function update_hits($table,$column,$id){
		return $this->query("UPDATE `".$table."` SET `".$column."`=".$column."+1 WHERE `id`='".$id."'");
	}
	
	public function id_exist($table,$id,$id2='id'){
		if($this->rows_num($this->query("SELECT ".$id2." FROM `".$table."` WHERE `".$id2."`='".$id."'")) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function create_table($table,$columns,$type,$extra='',$default='',$primary='id'){
		$ivar	= "CREATE TABLE `".$table."` (";
		$i	= 0;
		$i2	= 0;
		$count	 = count($columns);
		foreach($columns as $value){
		if(! empty($default[$i])){ $default[$i] = " default '.".$default[$i]."' "; }
			$i2++;
			$ivar	.= "`".$value."` ".$type[$i]." NOT NULL ".$extra[$i]." ".$default[$i]."
			";
			if($i2 <= $count){ $ivar	.= ','; }
			$i++;
		}
		$ivar	.= " PRIMARY KEY (`".$primary."`))
		TYPE=MyISAM AUTO_INCREMENT=1 ;";
		
		//echo $ivar,'<br />';
		if($this->query($ivar)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public function SUM($table,$field,$as='total',$Extra=''){
	$query	= $this->fetch($this->query("SELECT SUM(`".$field."`) as `".$as."` FROM `".$table."` ".$Extra.""));
	return $query[$as];
	
	}
	
	
	public function close(){
		return mysql_close();
	}
	# multi delete
}
?>