質問編集履歴

2

コードを修正しました。foreachでCSVファイルの値を取得。

2019/12/03 12:31

投稿

amaturePy
amaturePy

スコア131

test CHANGED
File without changes
test CHANGED
@@ -62,23 +62,7 @@
62
62
 
63
63
 
64
64
 
65
- <?php
65
+ <?php
66
-
67
-
68
-
69
- try {
70
-
71
- $file = new SplFileObject('/tmp/winneyuser.csv');
72
-
73
- $file->setFlags(SplFileObject::READ_CSV);
74
-
75
- }catch (RuntimeException $e) {
76
-
77
- throw $e;
78
-
79
- }
80
-
81
-
82
66
 
83
67
  try{
84
68
 
@@ -114,29 +98,41 @@
114
98
 
115
99
 
116
100
 
117
- $line_id -> $file[0];
101
+ $file = new SplFileObject('/tmp/winneyuser.csv');
118
102
 
119
- $username -> $file[1];
103
+ $file->setFlags(SplFileObject::READ_CSV);
120
104
 
121
-
105
+ $insert_array = [];
122
106
 
123
- foreach($file as $files) {
107
+ foreach ($file as $line) {
108
+
109
+ $insert_array[] = [
110
+
111
+ 'line_id' => $line[0],
112
+
113
+ 'username' => $line[1],
114
+
115
+ ];
116
+
117
+ }
118
+
119
+
124
120
 
125
121
  $stmt = $pdo->prepare("INSERT INTO users (line_id, name) VALUES (:lineid, :username)");
126
122
 
127
-
128
-
129
- $stmt->bindValue(':line_id', $line_id, PDO::PARAM_STR);
123
+ $stmt->bindValue(':lineid', $line_id, PDO::PARAM_STR);
130
124
 
131
125
  $stmt->bindValue(':username', $username, PDO::PARAM_STR);
132
126
 
127
+
128
+
129
+ foreach($insert_array as $insert_data) {
130
+
133
131
  $stmt->execute();
134
132
 
135
-
133
+ }
136
134
 
137
135
  $pdo->commit();
138
-
139
- }
140
136
 
141
137
  ?>
142
138
 

1

質問内容を編集

2019/12/03 12:31

投稿

amaturePy
amaturePy

スコア131

test CHANGED
File without changes
test CHANGED
@@ -84,11 +84,11 @@
84
84
 
85
85
  $pdo = new PDO(
86
86
 
87
- 'mysql:host=database-1.cuiejdfzi0uf.ap-northeast-1.rds.amazonaws.com;dbname=hitobito_data;charset=utf8mb4',
87
+ 'mysql**********',
88
88
 
89
- 'admin',
89
+ '******',
90
90
 
91
- 'hitobitoOwner',
91
+ '********',
92
92
 
93
93
  [
94
94