-
Notifications
You must be signed in to change notification settings - Fork 1
/
Update.php
87 lines (83 loc) · 2.61 KB
/
Update.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
<?php
namespace GetSqlFunc\Update;
class Update
{
private $stmt;
private $bind_mark;
private $bind;
private $e;
private $value;
function Stmt_Loader($value)
{
$this->stmt = $value;
}
public function Do($data)
{
$exception = true;
try {
if (isset($data['bind'])) {
$this->bind = $data['bind'];
}
$this->value = $data['value'];
if (!isset($data['clause'])) {
$sql = $this->GenerateSql(false, $data['key'], $data['table']);
}else{
$sql = $this->GenerateSql($data['clause'], $data['key'], $data['table']);
}
mysqli_stmt_prepare($this->stmt, $sql);
if (!$data['clause']) {
$this->stmt->bind_param($this->bind_mark, ...(array) $data['value']);
mysqli_stmt_execute($this->stmt);
goto common;
}
$this->stmt->bind_param($this->bind_mark, ...(array) array_merge($data['value'], $data['bind']));
mysqli_stmt_execute($this->stmt);
} catch (Exception $e) {
$exception = false;
$this->e = $e;
}
common:
if (mysqli_affected_rows($data['con']) == 0) {
$res = false;
} else {
$res = true;
}
if ($res && $exception && empty(mysqli_stmt_error($this->stmt))) {
return array('status' => true, 'reason' => null);
} else {
return array('status' => false, 'reason' => '执行失败或没有匹配的数据', 'error' => $this->e . ' ' . mysqli_stmt_error($this->stmt));
}
}
private function GenerateSql($clause, $args, $table)
{
$key_data = '';
foreach ($args as $tmp) {
if ($tmp !== end($args)) {
$key_data .= $tmp . '= ? , ';
} else {
$key_data .= $tmp . '= ? ';
}
}
foreach ($this->value as $tmp) {
if (is_int($tmp)) {
$this->bind_mark .= 'i';
} else {
$this->bind_mark .= 's';
}
}
if (!$clause) {
$sql = 'UPDATE ' . $table . ' SET ' . $key_data;
return $sql;
}
$mark_data = '';
foreach ($this->bind as $tmp) {
if (is_int($tmp)) {
$this->bind_mark .= 'i';
} else {
$this->bind_mark .= 's';
}
}
$sql = 'UPDATE ' . $table . ' SET ' . $key_data . ' WHERE ' . $clause;
return $sql;
}
}