<?php
class netsMySql extends netsConfig {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public $sysSPLIT1=',';
	
	public $sHost='localhost';
	public $sPort='3306';
	public $sUName='root';
	public $sPxd='admin';
	public $sDBName='nets_db';
	public $sCharset='utf-8';
	
	public $sysConnection;
	public $sysSQL='';
	public $sysSqlCount=0;
	public $sysPageInfo=array();
	
	public function netsMySql($pConnection){
		$this->netsConfig();
		$tmpAR0=explode($this->sysSPLIT1,$pConnection);
		if(count($tmpAR0)==6){
			$this->sHost=$tmpAR0[0];
			$this->sPort=$tmpAR0[1];
			$this->sUName=$tmpAR0[2];
			$this->sPxd=$tmpAR0[3];
			$this->sDBName=$tmpAR0[4];
			$this->sCharset=$tmpAR0[5];
		}else {
			echo ($pConnection.'Done<br />');
			return 1;
		}
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		mysql_select_db($this->sDBName);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		};
		//SET NAMES <Nets.Charset>
		$tmpSTR=' SET NAMES '.str_replace('-','',$this->sysCharset);
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		//SET AUTOCOMMIT=0;
		/*
		$tmpSTR=' SET AUTOCOMMIT=1; ';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'<br />'.$this->sysConnection,'0');
		}
		*/
	}
	
	public function TestConnection(){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		return 0;
	}
	public function TestDbExist(){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		mysql_select_db($this->sDBName);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		return 0;
	}
	public function Create($pDBName){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		$tmpSQL=' CREATE DATABASE {Nets.DBName} ';
		$tmpSTR=str_replace('{Nets.DBName}',$this->sDBName,$tmpSQL);
		$tmpResult=mysql_query($tmpSTR,$this->sysConnection);
		/*
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		*/
		
		return 0;
	}
	public function Search($pSQL){
		$tmpDataCollection=array();
		$tmpLine='';
		
		$tmpSTR=$pSQL;
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		$tmpResult=mysql_query($pSQL);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
		}
		$Coun0=0;
		while($tmpLine=mysql_fetch_array($tmpResult,MYSQL_ASSOC)){
			$tmpDataCollection[$Coun0]=$tmpLine;
			$Coun0++;
		}
		return  $tmpDataCollection;
	}
	public function Execute($pSQL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		
		$tmpSTR=$pSQL;
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		return 0;
	}
	public function Executes($pSQLS,$pIndex,$pSize,$pType){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Get index ------ --
		$tmpSQLS_AR=explode(';'.$this->sysSPLIT4,$pSQLS);
		array_pop($tmpSQLS_AR);
		$tmpCount=$pSize;
		$tmpStartIndex=$pIndex*$pSize;
		if ($pSize==0){
			$pSize=count($tmpSQLS_AR);
			$tmpStartIndex=0;
		}
		//-- ------ #Executes ------ --
		$Coun0=$tmpStartIndex;
		while (($tmpStartIndex+$pSize)>$Coun0 && count($tmpSQLS_AR)>$Coun0){
			$tmpSTR=$tmpSQLS_AR[$Coun0];
			$this->sysSqlCount++;
			mysql_query($tmpSTR,$this->sysConnection);
			if(mysql_error()!='' && $pType==1){
				$this->sysLastError=mysql_error();
				return 1;
			}
			$Coun0++;
		}
		
		return 0;
	}
	public function Close(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		
		mysql_close($this->sysConnection);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		return 0;
	}
}
?>