[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Оцените класс
Страницы: 1, 2
olimpset
Написал класс для работы с БД, скажите все ли верно я сделал, и надо что то поменять?
class DataBase
{
private $db_host = "localhost";
private $db_name = "uscriptmaker";
private $db_user = "root";
private $db_pass = "";
public $message;
private $numResults;
public $result;

public function format($val)
{
$this->connect();
if(is_array($val))
{
foreach ($val as $column => $values)
{
$val[$column] = mysql_real_escape_string("$val[$column]");
}
return $val;
}
else return $val = mysql_real_escape_string("$val");
}

public function connect()
{
$connect = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
$select_db = mysql_select_db($this->db_name);
@mysql_query("SET NAMES CP1251");
if($connect && $select_db) return true; else return false;
}
public function select_result($table,$rows = '*',$where)
{
if($this->connect())
{
$select = "SELECT $rows FROM $table WHERE $where";
$sql = mysql_query($select);
$sql = @mysql_query($select);
if($sql)
{
$row = mysql_fetch_assoc($sql);
if(empty($row)) return false; else return true;
}else return false;
}
}


public function insert($table,$values)
{
if($this->connect())
{
if(is_array($values)) $columns = implode(",",array_keys($values));
$values = implode("','",$this->format($values));
$ins = @mysql_query("INSERT INTO $table($columns) VALUES('$values')");
if($ins) return true; else return false;
} else return false;
}
public function delete($table,$where = null)
{
if($this->connect())
{
$delete = "DELETE FROM $table ";
if(is_array($where)) $where = implode(",",$where);
if($where != null) $delete .= "WHERE $where";
$sql_del = @mysql_query($delete);
if($sql_del) return true; else return false;

}
else return false;
}
public function update($table,$sets,$where)
{
if($this->connect())
{
$sets = implode(",",$sets);
$where = implode(",",$where);
if(is_array($sets))
$update = mysql_query("UPDATE $table SET $sets WHERE $where");
if($update) return true; else return false;

}
else return false;
}
public function select($table,$rows = null,$where = null,$order = null)
{
if($this->connect())
{
$select = "SELECT ";
if(is_array($rows)) $rows = implode(",",$rows);
if($rows != null) $select .= "$rows "; else $select .= "* ";
$select .= "FROM $table ";
if($where != null) $select .= "WHERE $where ";
if($order != null) $select .= "ORDER BY $order";
$sql = @mysql_query($select);
if($sql)
{
$this->result = mysql_fetch_assoc($sql);
return true;
}
else return false;
}
else false;
}
}
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.