-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcrud.php
111 lines (93 loc) · 3.2 KB
/
opcrud.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
111
<?php
// Function to execute insert query
function createData($mysqli, $table, $data) {
$columns = implode(", ", array_keys($data));
$values = implode(", ", array_fill(0, count($data), "?"));
$sql = "INSERT INTO $table ($columns) VALUES ($values)";
$stmt = $mysqli->prepare($sql);
if ($stmt) {
$stmt->bind_param(str_repeat('s', count($data)), ...array_values($data));
$stmt->execute();
$stmt->close();
return true;
}
return false;
}
// Function to retrieve data
function readData($mysqli, $table, $conditions = []) {
$sql = "SELECT * FROM $table";
if (!empty($conditions)) {
$sql .= " WHERE " . implode(" AND ", array_map(function($col) {
return "$col = ?";
}, array_keys($conditions)));
}
$stmt = $mysqli->prepare($sql);
if ($stmt) {
if (!empty($conditions)) {
$stmt->bind_param(str_repeat('s', count($conditions)), ...array_values($conditions));
}
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $data;
}
return [];
}
// Function to update data
function updateData($mysqli, $table, $data, $conditions) {
$setClause = implode(", ", array_map(function($col) {
return "$col = ?";
}, array_keys($data)));
$whereClause = implode(" AND ", array_map(function($col) {
return "$col = ?";
}, array_keys($conditions)));
$sql = "UPDATE $table SET $setClause WHERE $whereClause";
$stmt = $mysqli->prepare($sql);
if ($stmt) {
$params = array_merge(array_values($data), array_values($conditions));
$stmt->bind_param(str_repeat('s', count($params)), ...$params);
$stmt->execute();
$stmt->close();
return true;
}
return false;
}
// Function to delete data
function deleteData($mysqli, $table, $conditions) {
$whereClause = implode(" AND ", array_map(function($col) {
return "$col = ?";
}, array_keys($conditions)));
$sql = "DELETE FROM $table WHERE $whereClause";
$stmt = $mysqli->prepare($sql);
if ($stmt) {
$stmt->bind_param(str_repeat('s', count($conditions)), ...array_values($conditions));
$stmt->execute();
$stmt->close();
return true;
}
return false;
}
// Function to read data with JOIN
function readDataWithJoin($mysqli, $table1, $table2, $joinCondition, $fields = "*", $conditions = []) {
$fieldList = is_array($fields) ? implode(", ", $fields) : $fields;
$sql = "SELECT $fieldList FROM $table1 JOIN $table2 ON $joinCondition";
if (!empty($conditions)) {
$sql .= " WHERE " . implode(" AND ", array_map(function($col) {
return "$col = ?";
}, array_keys($conditions)));
}
$stmt = $mysqli->prepare($sql);
if ($stmt) {
if (!empty($conditions)) {
$stmt->bind_param(str_repeat('s', count($conditions)), ...array_values($conditions));
}
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $data;
}
return [];
}
?>