回答編集履歴

1

まとめサンプルコード

2018/04/05 01:07

投稿

m.ts10806
m.ts10806

スコア80850

test CHANGED
@@ -27,3 +27,93 @@
27
27
  //以下略
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ # まとめサンプルコード
34
+
35
+
36
+
37
+ ```php
38
+
39
+ class Database
40
+
41
+ {
42
+
43
+ protected $dbh;
44
+
45
+ function getDBH()
46
+
47
+ {
48
+
49
+ try{
50
+
51
+ $dsn = 'mysql:host=localhost; dbname=test1;charset=utf8;';
52
+
53
+ $user = 'root';
54
+
55
+ $password = 'root';
56
+
57
+ $this->dbh = new PDO($dsn, $user,$password);
58
+
59
+ $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
60
+
61
+ $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
62
+
63
+ $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
64
+
65
+ }catch(PDOException $e){
66
+
67
+ die($e->getMessage());
68
+
69
+ }
70
+
71
+ }
72
+
73
+ }
74
+
75
+
76
+
77
+ class DataSelect extends Database
78
+
79
+ {
80
+
81
+ function __construct()
82
+
83
+ {
84
+
85
+ $this->getDBH();
86
+
87
+ }
88
+
89
+ function test()
90
+
91
+ {
92
+
93
+ try{
94
+
95
+ $sql="SELECT * FROM tb";
96
+
97
+ $stmt = $this->dbh->prepare($sql);
98
+
99
+ $stmt->execute([]);
100
+
101
+ $rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
102
+
103
+ print_r($rows);
104
+
105
+ }catch(PDOException $e){
106
+
107
+ die($e->getMessage());
108
+
109
+ }
110
+
111
+ }
112
+
113
+ }
114
+
115
+ $myExec=new DataSelect;
116
+
117
+ $myExec->test();
118
+
119
+ ```