-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
91 lines (73 loc) · 3.13 KB
/
Database.php
File metadata and controls
91 lines (73 loc) · 3.13 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
<?php
/**
* Created by PhpStorm.
* User: Asus
* Date: 20.12.2016
* Time: 11:05
*/
class Database
{
private $local_opt, $conn, $db_errors, $result_data;
private $result_str;
private $defaults = array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'root',
'db' => 'schedule',
);
public function __construct($opt = array())
{
try {
$this->local_opt = array_merge($this->defaults, $opt); # мержим два массива
$this->db_errors = array();
$this->result_data = array();
$this->result_str = array();
@$this->conn = new mysqli($this->local_opt['host'], $this->local_opt['user'], $this->local_opt['pass'], $this->local_opt['db']);
} catch (Exception $e) {
if (!$this->conn) die($this->errors = 'Ошибка соединения с MYSQL: ошибка № ' . $this->conn->connect_errno . " " . $this->conn->connect_errno);
}
}
//проверить день в бд
final public function Add_day(string $tmp)
{
$this->Check_day($tmp);
# узнаем, есть ли такой день в бд; если нет, вперед-добавим
if (empty($this->result_data['name_day_of_week'])) {
# подготовка запроса
$stmt = $this->conn->prepare("INSERT INTO calendar(name_day_of_week,type_week) VALUES (?,?)");
for ($i = 1; $i < 3; $i++) { # вторая стадия: привязка параметров
$stmt->bind_param('si', $tmp, $i);
# выполнение запроса
if (!$stmt->execute()) {
$this->db_errors[] = "ошибка выполнения запроса" . $stmt->errno . " " . $stmt->error;
} else {
$stmt->store_result();# сохраняем результаты
}
}
$stmt->close();
} else {
echo "такой день уже добавлен =(<br>";
}
}
final private function Check_day(string $tmp)
{
# подготовка запроса
# mysql> SELECT * FROM [table name] WHERE [field name] = "whatever"
$stmt = $this->conn->prepare("SELECT name_day_of_week FROM calendar WHERE name_day_of_week=? ");
# вторая стадия: привязка параметров
$stmt->bind_param('s', $tmp);
if (!$stmt->execute()) {
$this->db_errors[] = "ошибка выполнения запроса" . $stmt->errno . " " . $stmt->error;
} else {
# выполняем запрос
$stmt->execute();
$stmt->store_result();# сохраняем результаты
# Определить переменные для записи результата
$stmt->bind_result($this->result_data['name_day_of_week']);
# получить найденные значения
$stmt->fetch();
# закрываем запрос
$stmt->close();
}
}
}