wykonałem taką klasę do obsługi DB:
class db{ protected $query; protected $no = 4; protected $limit; protected $table; protected $offset; protected $columns; protected $group; protected $where = ""; protected $order; protected $columns_query; private $connection; public function __construct(){ $this->query = "SELECT * FROM ".$this->table." "; foreach($this->columns AS $col){ $this->columns_query[] = $col[0]; } } public function __destruct(){ } public function getColumns($value){ $query = "SELECT "; $order = ""; foreach($value AS $col){ $query .= $col.", "; $this->columns_query[] = $col; } $query .= " FROM ".$this->table." "; $this->query = $query; return $this; } private function columnExist($start, $name){ $find = false; for($q=0; $q<$this->no; $q++){ $find = true; } } return $find; } public function __call($name, $value){ $col = $this->columnExist(8, $name); if($col!=1){ $this->where .= $col."='".$value[0]."' AND "; }else{ } $col = $this->columnExist(7, $name); if($col!=1){ }else{ } }else if($name=="setLimit"){ $this->limit = $value[0]; }else if($name=="setOffset"){ $this->offset = $value[0]; $col = $this->columnExist(7, $name); if($col!=1){ $this->group = $col; } } return $this; } public function where($where){ $this->query .= "(".$where.") AND "; return $this; } public function find(){ $this->query .= "WHERE ".$this->where." "; } if($this->group!=NULL){ $this->query .= "GROUP BY ".$this->group." "; } $this->query .= $this->order." "; if($this->offset!=NULL || $this->limit!=NULL){ $this->query .= "LIMIT "; if($this->offset!=NULL){ $this->query .= $this->offset.","; }else{ $this->query .= "0,"; } if($this->limit!=NULL){ $this->query .= $this->limit; }else{ $this->query .= "0"; } } $q = 0; foreach($this->columns_query AS $col){ $return[$q][$col] = $row[$col]; } $q++; } return $return; } }
Każda tabela z bazy danych musi być reprezentowana, przez jedną klasę, dziedziczącą po powyższej klasie. Przykładowo:
class db_uzytkownicy extends db{ protected $no = 4; // ilość kolumn w tabeli protected $table = "uzytkownicy"; // nazwa tabeli w bazie danych // spis kolumn w tabeli ); }
Jak oceniacie powyższy kod? Proszę o surowe i konstruktywne opinie: co poprawić, co polepszyć, co usunąć?
Pozdrawiam
