-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB.php
74 lines (67 loc) · 1.77 KB
/
DB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Предоставляет интерфейс-singleton к базе данных
*/
class DB {
/**
* @var null|PDO
*/
private $engine = null;
/**
* @var bool
*/
private static $instance = false;
/**
* Возвращает объект класса DB
* @return DB
*/
public static function getInstance () {
if (self::$instance === false) {
self::$instance = new DB;
}
return self::$instance;
}
/**
* Инициализация PDO
*/
private function __construct () {
try {
$this->engine = new PDO(DB_CONNECT_STRING, DB_USER, DB_PASSWORD);
$this->engine->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
print $e->getMessage();
}
}
/**
* Отключить PDO
*/
public function __destruct() {
$this->engine = null;
self::$instance = false;
}
/**
* Выполнить запрос к базе данных
* @param string $sQuery prepared строка с запросом
* @param array $data ассоциативный массив с данными для запроса
* @return PDOStatement
*/
public function query ($sQuery, $data = array()) {
try {
$result = $this->engine->prepare ($sQuery);
$result->setFetchMode(PDO::FETCH_ASSOC);
$result->execute($data);
}
catch(PDOException $e) {
print $e->getMessage();
}
return $result;
}
/**
* Получить id последнего вставленного элемента
* @return string
*/
public function lastInsertId(){
return $this->engine->lastInsertId();
}
}