teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2018/01/23 12:55

投稿

mashumaro0628
mashumaro0628

スコア19

title CHANGED
File without changes
body CHANGED
@@ -45,4 +45,31 @@
45
45
  echo $course_name_array[2]までは値がとれています
46
46
 
47
47
  ###補足情報(言語/FW/ツール等のバージョンなど)
48
+ create table Course (
49
+ Course_Number int(5) auto_increment,
50
+ Course_Name varchar(30) not null,
51
+ Release_Range boolean not null,
52
+ Maintenance date not null,
53
+ primary key(Course_Number));
54
+
55
+ insert into Course(Course_Name,Release_Range,Maintenance) values('グルーミング',true,'20171112');
56
+ insert into Course(Course_Name,Release_Range,Maintenance) values('トリミング',true,'20171112');
57
+ insert into Course(Course_Name,Release_Range,Maintenance) values('シャンプー',true,'20171112');
58
+ -----------------------------------------------------------------------
48
- より詳細な情報
59
+ create table Size (
60
+ Size_Number int(5) auto_increment,
61
+ Size_name varchar(20) not null,
62
+ Release_Range boolean not null,
63
+ Maintenance date not null,
64
+ primary key(Size_Number)
65
+ );
66
+
67
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('小型',true,20171112,'グルーミング');
68
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('中型',true,20171112,'グルーミング');
69
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('大型',true,20171112,'グルーミング');
70
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('小型',true,20171112,'トリミング');
71
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('中型',true,20171112,'トリミング');
72
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('大型',true,20171112,'トリミング');
73
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('小型',true,20171112,'シャンプー');
74
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('中型',true,20171112,'シャンプー');
75
+ insert into Size(Size_name,Release_Range,Maintenance,Course_name) values('大型',true,20171112,'シャンプー');

2

エラー修正

2018/01/23 12:54

投稿

mashumaro0628
mashumaro0628

スコア19

title CHANGED
File without changes
body CHANGED
@@ -6,11 +6,9 @@
6
6
  ###発生している問題・エラーメッセージ
7
7
 
8
8
  ```
9
- Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\training\aaa\index.php on line 26
9
+ Notice: Undefined variable: key1 in C:\xampp\htdocs\training\aaa\index.php on line 19
10
10
 
11
- Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\training\aaa\index.php on line 26
11
+ Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\training\aaa\index.php on line 19
12
-
13
- Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\training\aaa\index.php on line 26
14
12
  ```
15
13
 
16
14
  ###該当のソースコード

1

ソースコード修正

2018/01/23 11:42

投稿

mashumaro0628
mashumaro0628

スコア19

title CHANGED
File without changes
body CHANGED
@@ -19,33 +19,28 @@
19
19
  $course_name="";
20
20
  $course_name_array=array();
21
21
  $col_math=0;
22
+ $pdo = new PDO("mysql:host=localhost;dbname=doubutsu;charset=utf8",'root','admin');
22
23
 
23
-
24
24
  //コース名抽出
25
25
  try{
26
- $pdo = new PDO("mysql:host=localhost;dbname=doubutsu;charset=utf8",'root','admin');
27
- $stmt = $pdo -> query("SELECT course_name from Course");
26
+ $stmt = $pdo -> prepare("SELECT course_name from Course");
27
+ $stmt->execute();
28
28
  foreach($stmt as $key) {
29
29
  $course_name.="<td>".$key['course_name']."</td>";
30
30
  $course_name_array[]=$key['course_name'];
31
31
  }
32
- $pdo = null;
33
- }catch(PDOException $e){
34
- exit('データベース接続失敗' . $e->getMessage());
35
- }
36
- echo $course_name_array[2];
32
+ echo $course_name_array[2];
37
- foreach ($course_name_array as $key1) {
33
+ foreach ($course_name_array as $key1 ) {
38
- try{
39
- $pdo = new PDO("mysql:host=localhost;dbname=doubutsu;charset=utf8",'root','admin');
40
- $stmt = $pdo -> query("SELECT Size from Size where course_name='".$key1."'");
34
+ $stmt = $pdo -> prepare("SELECT Size from Size where course_name='".$key1."'");
35
+ $stmt->execute();
41
36
  foreach ($stmt as $key2) {
42
37
  $size_array=$key2['Size'];
43
38
  }
44
39
  $pdo = null;
40
+ }
45
- }catch(PDOException $e){
41
+ }catch(PDOException $e){
46
- exit('データベース接続失敗' . $e->getMessage());
42
+ exit('データベース接続失敗' . $e->getMessage());
47
- }
43
+ }
48
- }
49
44
  ```
50
45
 
51
46
  ###試したこと