回答編集履歴
2
もっとちゃんとした具体例を追記しました
answer
CHANGED
@@ -17,4 +17,67 @@
|
|
17
17
|
?>
|
18
18
|
```
|
19
19
|
|
20
|
-
引用元 http://php.net/manual/en/pdostatement.execute.php
|
20
|
+
引用元 http://php.net/manual/en/pdostatement.execute.php
|
21
|
+
|
22
|
+
|
23
|
+
追記2
|
24
|
+
|
25
|
+
SQLの周りのコードを綺麗にするならこうです
|
26
|
+
```php
|
27
|
+
//テストデータ
|
28
|
+
$example_name_sei = 'a';
|
29
|
+
$example_name_mei = 'b';
|
30
|
+
$example_name_seikana = 'a';
|
31
|
+
$example_name_meikana = 'b';
|
32
|
+
$example_age = 5;
|
33
|
+
$example_sex = 'm';
|
34
|
+
$example_hight = 60;
|
35
|
+
$example_weight = 70;
|
36
|
+
$example_company = 'hoge';
|
37
|
+
$example_tel1 = 111;
|
38
|
+
$example_tel2 = 111;
|
39
|
+
$example_address = 'hogehoge';
|
40
|
+
|
41
|
+
//これを別のところで作る
|
42
|
+
$hogedatas = array(
|
43
|
+
':name_sei' => $example_name_sei,
|
44
|
+
':name_mei' => $example_name_mei,
|
45
|
+
':name_seikana' => $example_name_seikana,
|
46
|
+
':name_meikana' => $example_name_meikana,
|
47
|
+
':age' => $example_age,
|
48
|
+
':sex' => $example_sex,
|
49
|
+
':hight' => $example_hight,
|
50
|
+
':weight' => $example_weight,
|
51
|
+
':company' => $example_company,
|
52
|
+
':tel1' => $example_tel1,
|
53
|
+
':tel2' => $example_tel2,
|
54
|
+
':address' => $example_address
|
55
|
+
);
|
56
|
+
|
57
|
+
function InsertRecord($array){
|
58
|
+
try{
|
59
|
+
$sql = 'INSERT INTO hogetable SET
|
60
|
+
name_sei = :name_sei,
|
61
|
+
name_mei = :name_mei,
|
62
|
+
name_seikana = :name_seikana,
|
63
|
+
name_meikana = :name_meikana,
|
64
|
+
age = :age ,
|
65
|
+
sex = :sex ,
|
66
|
+
hight = :hight,
|
67
|
+
weight = :weight,
|
68
|
+
company = :company,
|
69
|
+
tel1 = :tel1 ,
|
70
|
+
tel2 = :tel2 ,
|
71
|
+
address = :address ';
|
72
|
+
$stmh = $this->prepare($sql);
|
73
|
+
$stmh->execute($array);
|
74
|
+
}catch (Exception $ex) {
|
75
|
+
die($ex->getMessage());
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
//どっかで発火
|
81
|
+
InsertRecord($hogedatas);
|
82
|
+
|
83
|
+
```
|
1
具体例がなかったので載せました
answer
CHANGED
@@ -1,4 +1,20 @@
|
|
1
1
|
引数が大量……ですか?
|
2
2
|
見たところ別段多くないと思いますよ。
|
3
3
|
|
4
|
-
参考までに引数をまとめたいなら引数を一つだけにして、連想配列で渡すという方法はあります。
|
4
|
+
参考までに引数をまとめたいなら引数を一つだけにして、連想配列で渡すという方法はあります。
|
5
|
+
|
6
|
+
マニュアルのですが具体例です。
|
7
|
+
|
8
|
+
```php
|
9
|
+
<?php
|
10
|
+
/* Execute a prepared statement by passing an array of insert values */
|
11
|
+
$calories = 150;
|
12
|
+
$colour = 'red';
|
13
|
+
$sth = $dbh->prepare('SELECT name, colour, calories
|
14
|
+
FROM fruit
|
15
|
+
WHERE calories < :calories AND colour = :colour');
|
16
|
+
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
|
17
|
+
?>
|
18
|
+
```
|
19
|
+
|
20
|
+
引用元 http://php.net/manual/en/pdostatement.execute.php
|