回答編集履歴

2

もっとちゃんとした具体例を追記しました

2016/06/15 16:18

投稿

oskbt
oskbt

スコア1895

test CHANGED
@@ -37,3 +37,129 @@
37
37
 
38
38
 
39
39
  引用元 http://php.net/manual/en/pdostatement.execute.php
40
+
41
+
42
+
43
+
44
+
45
+ 追記2
46
+
47
+
48
+
49
+ SQLの周りのコードを綺麗にするならこうです
50
+
51
+ ```php
52
+
53
+ //テストデータ
54
+
55
+ $example_name_sei = 'a';
56
+
57
+ $example_name_mei = 'b';
58
+
59
+ $example_name_seikana = 'a';
60
+
61
+ $example_name_meikana = 'b';
62
+
63
+ $example_age = 5;
64
+
65
+ $example_sex = 'm';
66
+
67
+ $example_hight = 60;
68
+
69
+ $example_weight = 70;
70
+
71
+ $example_company = 'hoge';
72
+
73
+ $example_tel1 = 111;
74
+
75
+ $example_tel2 = 111;
76
+
77
+ $example_address = 'hogehoge';
78
+
79
+
80
+
81
+ //これを別のところで作る
82
+
83
+ $hogedatas = array(
84
+
85
+ ':name_sei' => $example_name_sei,
86
+
87
+ ':name_mei' => $example_name_mei,
88
+
89
+ ':name_seikana' => $example_name_seikana,
90
+
91
+ ':name_meikana' => $example_name_meikana,
92
+
93
+ ':age' => $example_age,
94
+
95
+ ':sex' => $example_sex,
96
+
97
+ ':hight' => $example_hight,
98
+
99
+ ':weight' => $example_weight,
100
+
101
+ ':company' => $example_company,
102
+
103
+ ':tel1' => $example_tel1,
104
+
105
+ ':tel2' => $example_tel2,
106
+
107
+ ':address' => $example_address
108
+
109
+ );
110
+
111
+
112
+
113
+ function InsertRecord($array){
114
+
115
+ try{
116
+
117
+ $sql = 'INSERT INTO hogetable SET
118
+
119
+ name_sei = :name_sei,
120
+
121
+ name_mei = :name_mei,
122
+
123
+ name_seikana = :name_seikana,
124
+
125
+ name_meikana = :name_meikana,
126
+
127
+ age = :age ,
128
+
129
+ sex = :sex ,
130
+
131
+ hight = :hight,
132
+
133
+ weight = :weight,
134
+
135
+ company = :company,
136
+
137
+ tel1 = :tel1 ,
138
+
139
+ tel2 = :tel2 ,
140
+
141
+ address = :address ';
142
+
143
+ $stmh = $this->prepare($sql);
144
+
145
+ $stmh->execute($array);
146
+
147
+ }catch (Exception $ex) {
148
+
149
+ die($ex->getMessage());
150
+
151
+ }
152
+
153
+ }
154
+
155
+
156
+
157
+
158
+
159
+ //どっかで発火
160
+
161
+ InsertRecord($hogedatas);
162
+
163
+
164
+
165
+ ```

1

具体例がなかったので載せました

2016/06/15 16:18

投稿

oskbt
oskbt

スコア1895

test CHANGED
@@ -5,3 +5,35 @@
5
5
 
6
6
 
7
7
  参考までに引数をまとめたいなら引数を一つだけにして、連想配列で渡すという方法はあります。
8
+
9
+
10
+
11
+ マニュアルのですが具体例です。
12
+
13
+
14
+
15
+ ```php
16
+
17
+ <?php
18
+
19
+ /* Execute a prepared statement by passing an array of insert values */
20
+
21
+ $calories = 150;
22
+
23
+ $colour = 'red';
24
+
25
+ $sth = $dbh->prepare('SELECT name, colour, calories
26
+
27
+ FROM fruit
28
+
29
+ WHERE calories < :calories AND colour = :colour');
30
+
31
+ $sth->execute(array(':calories' => $calories, ':colour' => $colour));
32
+
33
+ ?>
34
+
35
+ ```
36
+
37
+
38
+
39
+ 引用元 http://php.net/manual/en/pdostatement.execute.php