<?
class db{

//	function db($type, $tbl,$x1,$x2,$limit){	}
	
	private function conMe($do){
	//global $xX;
	$dbArr=array('h'=>'localhost','d'=>'bashro_itla','u'=>'bashro_itla','p'=>'BashItla11' );
	$sbLink=mysql_connect($dbArr['h'], $dbArr['u'],$dbArr['p']) or die(mysql_error('oh No!! we gotta a Problem :0'));
	mysql_query("set names 'utf8'"); 
//	mysql_select_db($xX->dbArr['d'],$sbLink) or die(mysql_error('oh No!! we gotta a Problem :(')); 
	mysql_select_db($dbArr['d']);
		if($do=='close'){mysql_close($sbLink);}
	}

	function select($what,$from,$where,$limit){
	$this->conMe('');
	$A="SELECT ".$what." FROM ".$from." ".$where." ".$limit."";
//	$A="SELECT * FROM wp_posts ";
	$A1=mysql_query($A);
	$back=array();
		if(!empty($A1)){
			while($A2=mysql_fetch_array($A1)){
			$back[]=$A2;
			}
			if (!empty($back)){	return $back;}
			else {return false;}
		}
	}
	

	
	
	function selectJ($what,$from,$jTBL,$jOnWhere,$jTBL2,$jOnWhere2,$where,$limit){
	$on2='';$j2='';
	if($jTBL2!='' && $jOnWhere2!=''){$on2='ON';$j2='JOIN';}

	$this->conMe('');
	$A="SELECT ".$what." FROM ".$from." JOIN  ".$jTBL." ON  ".$jOnWhere."   ".$j2."       ".$jTBL2." ".$on2."  ".$jOnWhere2." " .$where." ".$limit."";
	$A1=mysql_query($A);
	$back=array();
		if(!empty($A1)){
			while($A2=mysql_fetch_array($A1)){
			$back[]=$A2;
			}
			if (!empty($back)){	return $back;}
			else {return false;}
		}
	}
	
	function insert($into,$vars,$vals){
	$this->conMe('');
//	$vals=mysql_real_escape_string($vals);
	 $A="INSERT INTO ".$into." ( ".$vars." ) VALUES( ".$vals." )";
		if ($A1=mysql_query($A)){
//		 $this->conMe('close');
		return true;	
		}
		else {
		//$this->conMe('close');
		 return false;
		 }
	}
	function insertMulti($into,$vars,$vals){
	$this->conMe('');
	 $A="INSERT INTO ".$into." ( ".$vars." ) VALUES ".$vals." ";
		if ($A1=mysql_query($A)){
		return true;	
		}
		else {
		 return false;
		 }
	}	
	
	
	function insCommit($into,$vars,$vals,$into2,$vars2,$vals2,$ins2ID){
	$this->conMe('');
	mysql_query("BEGIN");
	 $A="INSERT INTO ".$into." ( ".$vars." ) VALUES( ".$vals." )";
		if ($A1=mysql_query($A)){
		$rID= mysql_insert_id();
		$B="INSERT INTO ".$into2." ( ".$ins2ID.",".$vars2." ) VALUES( \"".$rID." \"".$vals2." )";
			if ($B1=mysql_query($B)){
			mysql_query("COMMIT"); 
			$this->conMe('close');
			return true;				
			}
			else{
			mysql_query("COMMIT"); 
			$this->conMe('close');
			return false;				
			}
		}
		else { mysql_query("COMMIT");  $this->conMe('close');return false;}
	}
	
	
	function update($tbl,$set,$where){
	$this->conMe('');
//	$A="UPDATE ".$tbl." SET  ".$set." ".$where." ";
	$A="UPDATE ".$tbl." SET  ".$set." ".$where." ";
		if ($A1=mysql_query($A)){
		 $this->conMe('close');
		return true;	
		}
		else { $this->conMe('close');return false;}
	}
	function delete($tbl,$w){
	$this->conMe('');
	$A="DELETE FROM ".$tbl." ".$w." ";
		if ($A1=mysql_query($A)){
		 $this->conMe('close');
		return true;	
		}
		else { $this->conMe('close');return false;}
	}
	
	
	function insCommArr($into,$vars,$vals,$into2,$vars2,$vals2,$ins2ID){
	$this->conMe('');
	mysql_query("BEGIN");
	 $A="INSERT INTO ".$into." ( ".$vars." ) VALUES (".$vals.") ";
		if ($A1=mysql_query($A)){
		$rID= mysql_insert_id();
			$valSTR='';
			foreach($vals2 as $i=>$v){
				if($i>0){$valSTR.=',';}
			$valSTR.='("'.$rID.'","'.$v.'")';			
			}
		$B="INSERT INTO ".$into2." ( ".$ins2ID.",".$vars2." ) VALUES ".$valSTR."";
			if ($B1=mysql_query($B)){
			mysql_query("COMMIT"); 
			$this->conMe('close');
			return true;				
			}
			else{
			mysql_query("ROLLBACK"); 
			$this->conMe('close');
			return false;
			}
		}
		else { mysql_query("ROLLBACK");  $this->conMe('close');	return false;}
	}
	
	
	
	


   //prevent injection
    function qry($query) {
      $this->dbconnect();
      $args  = func_get_args();
      $query = array_shift($args);
      $query = str_replace("?", "%s", $query);
      $args  = array_map('mysql_real_escape_string', $args);
      array_unshift($args,$query);
      $query = call_user_func_array('sprintf',$args);
      $result = mysql_query($query) or die(mysql_error());
          if($result){
            return $result;
          }else{
             $error = "Error";
             return $result;
          }
    }
	private function arrFromDB($str,$type){
	$back='';
	$count=0;
	$bArr=array();
	$arr=explode(',',$str);
		foreach( $arr as $i=>$v){
			if ($v==''){}
			else {
			$count++;
				if ($type=='homeItems'){
					if ($count==1){$back.=' WHERE id='.$v.' ';}
					else{$back.='OR id='.$v.' ';}
				}
				elseif($type=='itemImages'){$bArr[$i-1]=$v;}
				elseif($type=='moreItems'){ return $arr; exit;}
				elseif($type=='moreArti'){ return $arr; exit;}
				else {return $arr; exit;}
			}
		}
	if($type=='itemImages'){return $bArr;}	
	else {return $back;	}
	}

}
?>
