-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
86 lines (75 loc) · 2.1 KB
/
Database.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
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class Database
{
private $db_connect;
private $error;
private $result;
public function __construct($host, $login, $password, $db)
{
$this->db_connect = mysqli_connect($host, $login, $password, $db);
mysqli_set_charset($this->db_connect, 'utf8');
if (!$this->db_connect) {
$this->error = mysqli_connect_error();
}
}
/**
* Подготовленый запрос к базе данных с переданными параметрами
* @param $sql - запрос к базе данных
* @param array $data - массив параметров
* @return bool
*/
public function executeQuery($sql, $data = [])
{
$this->error = null;
$stmt = db_get_prepare_stmt($this->db_connect, $sql, $data);
if (mysqli_stmt_execute($stmt) && $r = mysqli_stmt_get_result($stmt)) {
$this->result = $r;
$res = true;
} else {
$this->error = mysqli_error($this->db_connect);
$res = false;
}
return $res;
}
/**
* Возвращает последнию ошибку
* @return null|string
*/
public function getError()
{
return $this->error;
}
/**
* Возвращает массив элементов из базы
* @return array|null
*/
public function getResultArray()
{
return mysqli_fetch_all($this->result, MYSQLI_ASSOC);
}
/**
* Возвращает ID
* @return int|string
*/
public function getID()
{
return mysqli_insert_id($this->db_connect);
}
/**
* Возвращает число элементов
* @return int
*/
public function getNumRows()
{
return mysqli_num_rows($this->result);
}
/**
* Возвращает очищенный текст от инъекций
* @param $str - входной параметр
* @return string
*/
public function getEscapeStr($str)
{
return $this->db_connect->real_escape_string($str);
}
}