<?php
class DB{
private $dsn;
private $settings;
private $connection = false;
public function __construct($options = null) {
$this->settings = parse_ini_file(CORE_PATH . DS . 'config' . DS . 'db.ini.php');
$this->dsn = $this->settings["db_driver"] . ':host=' . $this->settings["db_host"] . ';dbname=' . $this->settings["db_name"];
$this->connection = new PDO($this->dsn, $this->settings["db_user"],$this->settings["db_pass"], $options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->exec("set names utf8");
}
public function select($db, $cols, $where = null, $order = null, $limit = null) {
$sql = "SELECT {$cols} FROM `{$db}` {$where} {$order} {$limit}";
$result = $this->connection->query($sql);
$result->execute();
if($where != null){
$row = $result->fetchAll(PDO::FETCH_ASSOC);
return $row;
} else {
$row = $result->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
}
public function insert($db, $cols = "", $values = "") {
if($cols == "") echo "Ошибка!";
$sql = "INSERT INTO `{$db}`({$cols}) VALUES ({$values})";
$this->connection->query($sql);
}
public function update($db, $what, $value, $where) {
$sql = "UPDATE `{$db}` SET {$what}={$value} {$where}";
$this->connection->query($sql);
}
public function delete($db,$where = "") {
$sql = "DELETE FROM `{$db}` {$where}";
return $this->connection->query($sql);
}
public function __destruct() {
$this->connection = null;
}
}