-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
84 lines (49 loc) · 1.67 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
75
76
77
78
79
80
81
82
83
84
<?php
$dbservername = "localhost";
$dbusername = "todo_list";
$dbpassword = "01todo_list";
$dbdatabase = "todo_list";
// $dbtable = "tasks";
// $dbtablefields = "`id` INT DEFAULT NULL AUTO_INCREMENT,
// `title` VARCHAR(255) DEFAULT NULL,
// `description` TEXT DEFAULT NULL,
// PRIMARY KEY(id)";
function sql_init(string $dbservername, string $dbusername, string $dbpassword, string $dbdatabase): mysqli
{
$conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbdatabase);
if ($conn->error) die("Database connection error: ".$conn->error);
return $conn;
}
function giveme(mysqli $conn, string $table, string $column, string $value): array
{
$sql = "SELECT * FROM `$table` WHERE $column = ?";
$stmt = $conn->prepare($sql);
if (!$stmt) return ["Database error!"];
$stmt->bind_param('s', $value);
$stmt->execute();
$response = $stmt->get_result();
$stmt->close();
$result = [];
if ($response->num_rows > 0){
while ($row = $response->fetch_assoc()){
$result[] = ["word" => $row["word"], "translate" => $row["translate"]];
}
}
return $result;
}
function giveall(mysqli $conn, string $table): array
{
$sql = "SELECT * FROM `$table`";
$stmt = $conn->prepare($sql);
if (!$stmt) return ["Database error!"];
$stmt->execute();
$response = $stmt->get_result();
$stmt->close();
$result = [];
if ($response->num_rows > 0){
while ($row = $response->fetch_assoc()){
$result[] = ["word" => $row["word"], "translate" => $row["translate"]];
}
}
return $result;
}