-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_exist.php
59 lines (46 loc) · 1.17 KB
/
record_exist.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
<?php
function checkRecordExists($tableName, $conditions) {
global $connect;
$sql = "SELECT COUNT(*) as count FROM $tableName WHERE ";
$whereClauses = [];
foreach ($conditions as $condition) {
$column = $condition['column'];
$operator = $condition['operator'];
$value = $condition['value'];
$logic = isset($condition['logic']) ? $condition['logic'] : 'AND';
$whereClauses[] = "$column $operator ?";
$params[] = $value;
$sql .= "$logic ";
}
$sql .= implode(" $logic ", $whereClauses);
$stmt = $connect->prepare($sql);
$stmt->execute($params);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['count'] > 0;
}
//usage
$conditions = [
[
'column' => 'email',
'operator' => '=',
'value' => $email
],
[
'column' => 'username',
'operator' => '=',
'value' => $username,
'logic' => 'OR'
],
[
'column' => 'age',
'operator' => '>=',
'value' => $age,
'logic' => 'AND'
]
];
if (checkRecordExists('users', $conditions)) {
// Record exists
} else {
// Record does not exist
}
?>