-
Notifications
You must be signed in to change notification settings - Fork 1
/
Insert.php
81 lines (80 loc) · 2.37 KB
/
Insert.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
<?php
namespace GetSqlFunc\Insert;
class Insert
{
private $stmt;
private $bind_mark;
private $value;
function Stmt_Loader($value)
{
$this->stmt = $value;
}
public function Do($data)
{
try {
if (isset($data['value'])) {
$this->value = false;
}
$sql = $this->GenerateSql($data['key'], $data['value'], $data['table']);
mysqli_stmt_prepare($this->stmt, $sql);
if (!$data['value']) {
try {
$res = mysqli_stmt_execute($this->stmt);
} catch (Exception $e) {
$res = false;
}
goto common;
}
$this->stmt->bind_param($this->bind_mark, ...(array) $data['value']);
$res = mysqli_stmt_execute($this->stmt);
} catch (Exception $e) {
$res = false;
}
common:
if ($res && empty(mysqli_stmt_error($this->stmt))) {
return array('status' => true, 'reason' => null);
} else {
return array('status' => false, 'reason' => '查询失败','err'=>$e.' '.mysqli_stmt_error($this->stmt));
}
}
private function GenerateSql($skey, $value, $table)
{
$key_data = '';
foreach ($skey as $tmp) {
if ($tmp !== end($skey)) {
$key_data .= $tmp . ', ';
} else {
$key_data .= $tmp;
}
}
if (!$value) {
$sql = 'INSERT INTO ' . $table . ' ( ' . $key_data . ' ) ';
return $sql;
}
$bind_data = '';
$mark_data = '';
$tmpnum = 0;
$notend = true;
$count = count($value) - 1;
foreach ($value as $tmp) {
if($tmpnum == $count){
$notend = false;
}
if (is_int($tmp)) {
$this->bind_mark .= 'i';
} else {
$this->bind_mark .= 's';
}
if ($notend) {
$bind_data .= $tmp . ', ';
$mark_data .= '? ,';
} else {
$bind_data .= $tmp;
$mark_data .= '?';
}
$tmpnum++;
}
$sql = 'INSERT INTO ' . $table . ' ( ' . $key_data . ' ) VALUES ( ' . $mark_data . ' )';
return $sql;
}
}