-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.php
110 lines (87 loc) · 2.93 KB
/
config.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
define("DB_SERVER", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "roles");
class Database
{
private $conn;
public function __construct()
{
$this->conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($this->conn->connect_error) {
die("Database connection error: " . $this->conn->connect_error);
}
}
public function __destruct()
{
if ($this->conn) {
$this->conn->close();
}
}
public function executeQuery($sql, $params = [], $types = "")
{
$result = $this->conn->prepare($sql);
if (!$result) {
return "SQL error: " . $this->conn->error;
}
if ($params) {
$result->bind_param($types, ...$params);
}
if (!$result->execute()) {
return "Execution error: " . $result->error;
}
return $result;
}
function validate($value)
{
return htmlspecialchars(trim(stripslashes($value)), ENT_QUOTES, 'UTF-8');
}
public function select($table, $columns = "*", $condition = "", $params = [], $types = "")
{
$sql = "SELECT $columns FROM $table" . ($condition ? " WHERE $condition" : "");
$result = $this->executeQuery($sql, $params, $types);
if (is_string($result)) {
return $result;
}
return $result->get_result()->fetch_all(MYSQLI_ASSOC);
}
public function insert($table, $data)
{
$keys = implode(', ', array_keys($data));
$placeholders = implode(', ', array_fill(0, count($data), '?'));
$sql = "INSERT INTO $table ($keys) VALUES ($placeholders)";
$types = str_repeat('s', count($data));
$result = $this->executeQuery($sql, array_values($data), $types);
if (is_string($result)) {
return $result;
}
return $this->conn->insert_id;
}
public function update($table, $data, $condition = "", $params = [], $types = "")
{
$set = implode(", ", array_map(function ($k) {
return "$k = ?";
}, array_keys($data)));
$sql = "UPDATE $table SET $set" . ($condition ? " WHERE $condition" : "");
$types = str_repeat('s', count($data)) . $types;
$result = $this->executeQuery($sql, array_merge(array_values($data), $params), $types);
if (is_string($result)) {
return $result;
}
return $this->conn->affected_rows;
}
public function delete($table, $condition = "", $params = [], $types = "")
{
$sql = "DELETE FROM $table" . ($condition ? " WHERE $condition" : "");
$result = $this->executeQuery($sql, $params, $types);
if (is_string($result)) {
return $result;
}
return $this->conn->affected_rows;
}
public function hashPassword($password)
{
return hash_hmac('sha256', $password, 'iqbolshoh');
}
}