db_class.php 3.12 KB
<?
class DBCLass
{

// Поля класса
 		
	private $server,$user,$password,$dbname,$db;

// Метод открытия соединения с MySQL (вызывается автоматически)

function __construct($server,$user,$password,$dbname)
{
	$this->server = $server;
	$this->user = $user;
	$this->password = $password;
	$this->dbname = $dbname;
	$this->openConnection();
}

// Метод подключения к бд

public function openConnection()
{
 	if(!$this->db)
 	{
		$connection = @mysql_connect($this->server,$this->user,$this->password);
		if($connection)
		{
			$selectDB = @mysql_select_db($this->dbname,$connection);
			if($selectDB)
			{
				$this->db = true;
				mysql_query('SET NAMES UTF8');
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	} 
	else
	{
		return true;
	}
}

// Выбор значений

public function select($what,$from,$where = null,$order = null)
{
	$fetched = array();
	$sql = 'SELECT '.$what.' FROM '.$from;
	if($where != null) $sql .= ' WHERE '.$where;
	if($order != null) $sql .= ' ORDER BY '.$order; 
 	
	$query = mysql_query($sql);
	if($query)
	{
		$rows = mysql_num_rows($query);
		for($i = 0; $i < $rows; $i++)
		{
			$results = mysql_fetch_assoc($query);
			$key = array_keys($results);
			$numKeys = count($key);
			for($x = 0; $x < $numKeys; $x++)
			{
				$fetched[$i][$key[$x]] = $results[$key[$x]];
			}
		}
		return $fetched;
		
	}
	else
	{
		return false;		
	}	
}

// Добавление записи в базу

public function insert($table,$values,$rows = null)
{
	$insert = 'INSERT INTO '.$table;
			
	$rows = implode(',',$rows);	
	$insert .= ' ('.$rows.')';	
	$numValues = count($values);
	
	for($i = 0; $i < $numValues; $i++)
	{
		if(is_string($values[$i])) $values[$i] = '"'.$values[$i].'"';
		if(empty($values[$i])) $values[$i] = '"'. Null.'"';
	}
	$values = implode(',',$values);
	$insert .= ' VALUES ('.$values.')';
	
	$ins = mysql_query($insert);
	return ($ins) ? true : false;	
}

// Обновление записи
public function update($table,$values,$rows,$where = null)
{
	$numValues = count($values);
	$numValues_2 = count($rows);
	
	if ($numValues == $numValues_2)
	{			
		$update = "UPDATE ".$table;
	
		$update .= " SET ";
				
		for($i = 0; $i < $numValues; $i++)
		{
			if(is_string($values[$i])) $values[$i] = "'".$values[$i]."'";	
			
			$update .= $rows[$i];
			$update .= " = ";
			if(empty($values[$i])) $values[$i] = '"'. Null.'"';
			$update .= $values[$i];
			
			
			if ($numValues> $i+1)
			{
				$update .= ", ";
			}
		}	
								
		if($where != null)
		{
			$update .= " WHERE ".$where;
		}
		
		$update = mysql_query($update);
	}
	return ($update) ? true : false;	
}

// Удаление записи
public function delete($table,$where = null)
{	
	$sql = 'DELETE FROM '.$table.' WHERE '.$where;
	if($where == null)
	{
		$sql = 'DELETE '.$table;
	}		

	$deleted = @mysql_query($sql);
	return ($deleted)? true : false;
}

// Закрыть соединение
public function closeConnection()
{
	if($this->db)
	{
		if(@mysql_close())
		{
			$this->db = false;
			return true;
		}
		else
		{
			return false;
		}
	}
}
} 
?>