-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
117 lines (97 loc) · 3.83 KB
/
database.php
File metadata and controls
117 lines (97 loc) · 3.83 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
DEFINE("MYSQL_HOST","localhost");
DEFINE("MYSQL_DATABASE","okki");
DEFINE("MYSQL_USER","root");
DEFINE("MYSQL_PASSWORD","");
class SimpleMySQL {
private $mysqli;
private $inTransaction = false;
public function __construct() {
$connectHost = MYSQL_HOST;
$connectDatabase = MYSQL_DATABASE;
$connectUser = MYSQL_USER;
$connectPassword = MYSQL_PASSWORD;
// Подключаемся к базе данных через mysqli
$this->mysqli = new mysqli($connectHost, $connectUser, $connectPassword, $connectDatabase);
// Проверяем подключение
if ($this->mysqli->connect_error) {
die("Connection failed: " . $this->mysqli->connect_error);
}
// Устанавливаем кодировку
$this->mysqli->set_charset("utf8");
$this->mysqli->autocommit(false);
}
public function getError() {
return "MySQL Error ({$this->mysqli->errno}): {$this->mysqli->error}";
}
// Выполнение запроса
public function query($sql, $params = []) {
$stmt = $this->mysqli->prepare($sql);
if ($stmt === false) {
die("Prepare failed: " . $this->mysqli->error);
}
if (!empty($params)) {
$types = str_repeat('s', count($params)); // Определение типов данных для привязки
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
return $stmt;
}
// Получение всех данных
public function fetchAll($sql, $params = []) {
$stmt = $this->query($sql, $params);
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
// Получение одной строки
public function fetch($sql, $params = []) {
$stmt = $this->query($sql, $params);
$result = $stmt->get_result();
return $result->fetch_assoc();
}
// Выполнение запроса без получения результата
public function execute($sql, $params = []) {
$this->query($sql, $params);
}
// Получение последнего ID вставленной записи
public function lastInsertId() {
return $this->mysqli->insert_id;
}
// Закрытие соединения с базой данных
public function close() {
$this->mysqli->close();
}
// Чтение кэша
public function readCache($filename) {
if (file_exists($filename)) {
$data = file_get_contents($filename);
return json_decode($data, true); // Второй параметр true для возврата массива, а не объекта
} else {
return null; // Можно обработать ситуацию, когда файл не существует
}
}
public function start() {
if ($this->inTransaction) {
return; // Уже начата, пропускаем
}
if ($this->mysqli->query("START TRANSACTION")) {
$this->inTransaction = true;
}
}
public function finish() {
if ($this->inTransaction && $this->mysqli->query("COMMIT")) {
$this->inTransaction = false;
}
}
public function stop() {
if ($this->inTransaction && $this->mysqli->query("ROLLBACK")) {
$this->inTransaction = false;
}
}
// Получение переменной сервера
public function getVariable($variableName) {
$stmt = $this->query("SHOW VARIABLES LIKE ?", [$variableName]);
return $stmt->get_result()->fetch_assoc()['Value'];
}
}
?>