回答編集履歴
5
修正
test
CHANGED
@@ -27,6 +27,8 @@
|
|
27
27
|
```php
|
28
28
|
|
29
29
|
function connection(){
|
30
|
+
|
31
|
+
return new PDO(xxxxx); //ここは自身の設定を反映すること
|
30
32
|
|
31
33
|
}
|
32
34
|
|
4
修正
test
CHANGED
@@ -154,7 +154,9 @@
|
|
154
154
|
|
155
155
|
|
156
156
|
|
157
|
-
$stmt = $pdo->prepare(implode(';',$sqls)
|
157
|
+
$stmt = $pdo->prepare(implode(';',$sqls),
|
158
|
+
|
159
|
+
array(PDO::MYSQL_ATTR_MULTI_STATEMENTS=>true));
|
158
160
|
|
159
161
|
stmtBind($stmt,array_merge($set,$set2));
|
160
162
|
|
3
修正
test
CHANGED
@@ -34,8 +34,6 @@
|
|
34
34
|
|
35
35
|
{
|
36
36
|
|
37
|
-
$pdo = connection();
|
38
|
-
|
39
37
|
$clms = [];
|
40
38
|
|
41
39
|
$values = [];
|
@@ -70,11 +68,17 @@
|
|
70
68
|
|
71
69
|
1個ずつ実行
|
72
70
|
|
71
|
+
---
|
72
|
+
|
73
73
|
```php
|
74
74
|
|
75
75
|
try{
|
76
76
|
|
77
77
|
$pdo = connection();
|
78
|
+
|
79
|
+
$pdo->beginTransaction();
|
80
|
+
|
81
|
+
|
78
82
|
|
79
83
|
$table = 'DB01';
|
80
84
|
|
@@ -122,11 +126,17 @@
|
|
122
126
|
|
123
127
|
まとめて実行
|
124
128
|
|
129
|
+
---
|
130
|
+
|
125
131
|
```php
|
126
132
|
|
127
133
|
try{
|
128
134
|
|
129
135
|
$pdo = connection();
|
136
|
+
|
137
|
+
$pdo->beginTransaction();
|
138
|
+
|
139
|
+
|
130
140
|
|
131
141
|
$sqls = [];
|
132
142
|
|
2
修正
test
CHANGED
@@ -13,3 +13,157 @@
|
|
13
13
|
|
14
14
|
|
15
15
|
あと既に指摘があるようにパラメータだけ指定して[bindValue](https://www.php.net/manual/ja/pdostatement.bindvalue.php)による値のbindが行われていないためのエラーが起きてるのではないでしょうか。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
サンプル(※動作未検証)
|
20
|
+
|
21
|
+
----
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
関数を用意
|
26
|
+
|
27
|
+
```php
|
28
|
+
|
29
|
+
function connection(){
|
30
|
+
|
31
|
+
}
|
32
|
+
|
33
|
+
function createInsertSQL(string $table,array $item):string
|
34
|
+
|
35
|
+
{
|
36
|
+
|
37
|
+
$pdo = connection();
|
38
|
+
|
39
|
+
$clms = [];
|
40
|
+
|
41
|
+
$values = [];
|
42
|
+
|
43
|
+
foreach($item as $clm=>$value){
|
44
|
+
|
45
|
+
$clms[] = $clm;
|
46
|
+
|
47
|
+
$values[] = ':'.$clm;
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
return "insert into {$table} (".implode(',',$clms).") VALUES (".implode(',',$values).")";
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
function stmtBind(PDOStatement $stmt,array $item)
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
foreach($item as $clm=>$value){
|
60
|
+
|
61
|
+
$stmt->bindValue(':'.$clm, $value);
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
1個ずつ実行
|
72
|
+
|
73
|
+
```php
|
74
|
+
|
75
|
+
try{
|
76
|
+
|
77
|
+
$pdo = connection();
|
78
|
+
|
79
|
+
$table = 'DB01';
|
80
|
+
|
81
|
+
$set = ['item1'=>1,'item2'=>2];
|
82
|
+
|
83
|
+
$stmt = $pdo->prepare(createInsertSQL($table,$set));
|
84
|
+
|
85
|
+
stmtBind($stmt,$set);
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
if(!$stmt->execute()){
|
90
|
+
|
91
|
+
throw new PDOException();
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
$table = 'DB02';
|
96
|
+
|
97
|
+
$set = ['item3'=>3,'item4'=>4];
|
98
|
+
|
99
|
+
$stmt = $pdo->prepare(createInsertSQL($table,$set));
|
100
|
+
|
101
|
+
stmtBind($stmt,$set);
|
102
|
+
|
103
|
+
if(!$stmt->execute()){
|
104
|
+
|
105
|
+
throw new PDOException();
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
$pdo->commit();
|
110
|
+
|
111
|
+
}catch(PDOException $e){
|
112
|
+
|
113
|
+
$pdo->rollBack();
|
114
|
+
|
115
|
+
die(json_encode($e));
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
まとめて実行
|
124
|
+
|
125
|
+
```php
|
126
|
+
|
127
|
+
try{
|
128
|
+
|
129
|
+
$pdo = connection();
|
130
|
+
|
131
|
+
$sqls = [];
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
$set1 = ['item1'=>1,'item2'=>2];
|
136
|
+
|
137
|
+
$sqls[] = createInsertSQL('DB01',$set1);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
$set2 = ['item3'=>3,'item4'=>4];
|
142
|
+
|
143
|
+
$sqls[] = createInsertSQL('DB02',$set2);
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
$stmt = $pdo->prepare(implode(';',$sqls));
|
148
|
+
|
149
|
+
stmtBind($stmt,array_merge($set,$set2));
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if(!$stmt->execute()){
|
154
|
+
|
155
|
+
throw new PDOException();
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
$pdo->commit();
|
160
|
+
|
161
|
+
}catch(PDOException $e){
|
162
|
+
|
163
|
+
$pdo->rollBack();
|
164
|
+
|
165
|
+
die(json_encode($e));
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
```
|
1
修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
「可変になるところ」を引数にした関数を作ると良いです。
|
10
10
|
|
11
|
-
そこは考えてやってみてください。
|
11
|
+
そこは考えてやってみてください。(「可能か?」という質問なので)
|
12
12
|
|
13
13
|
|
14
14
|
|