質問編集履歴

1

質問内容の詳細を記述

2020/08/05 10:54

投稿

imawork
imawork

スコア6

test CHANGED
@@ -1 +1 @@
1
- クラスインス変数の基本は学んだのすが、応用方法がわかりません。
1
+ Fatal error: Uncaught Error: Call to a member function execute() on string in 〜と出てデーベーに接続ません。
test CHANGED
@@ -1,8 +1,14 @@
1
- 以下のコードを、global変数を使用せずに完成させたいです。
1
+ 初心者です。Fatal error: Uncaught Error: Call to a member function execute() on string in /Applications/MAMP/htdocs/keijiban1/model.php:24 Stack trace: #0 /Applications/MAMP/htdocs/keijiban1/comment.php(23): Db::outputdb() #1 {main} thrown in /Applications/MAMP/htdocs/keijiban1/model.php on line 24
2
+
3
+ と言うエラー表示が出ており、データベースに接続できていないようです。
4
+
5
+ 以下がcomment.phpと、model.phpのコードです。
6
+
7
+
2
8
 
3
9
  - comment.php
4
10
 
5
-  ```php
11
+ ```php
6
12
 
7
13
  <?php
8
14
 
@@ -20,9 +26,9 @@
20
26
 
21
27
  print('投稿しました<br>');
22
28
 
29
+ $db=new Db;
30
+
23
- inputdb();
31
+ $db->inputdb();
24
-
25
-
26
32
 
27
33
  }else{
28
34
 
@@ -46,13 +52,15 @@
46
52
 
47
53
  $start=5*($page-1);
48
54
 
55
+ $db=new Db;
56
+
49
- outputdb();
57
+ $db->outputdb();
58
+
59
+
60
+
50
-
61
+ //コメント、時間を繰り返し表示
51
-
52
-
53
-
54
-
62
+
55
- while($comment=$comments->fetch()){
63
+ while($comment=self::$comments->fetch()){
56
64
 
57
65
  print('<p>');
58
66
 
@@ -76,7 +84,7 @@
76
84
 
77
85
 
78
86
 
79
-
87
+ ////2ページ目以降なら前のページを表示する(1ページ目は表示しない)
80
88
 
81
89
  if($page>=2){
82
90
 
@@ -88,15 +96,17 @@
88
96
 
89
97
 
90
98
 
91
-
99
+ //最終ページは表示コメント数/5の繰り上げで表示
100
+
92
-
101
+ $db=new Db;
102
+
93
- countdb();
103
+ $db->countdb();
94
104
 
95
105
 
96
106
 
97
107
  $max_page=ceil($count['cnt']/5);
98
108
 
99
-
109
+ //現在のページ<最終ページなら次ページを表示する
100
110
 
101
111
  if($page<$max_page){
102
112
 
@@ -108,6 +118,8 @@
108
118
 
109
119
  ?>
110
120
 
121
+
122
+
111
123
  ```
112
124
 
113
125
  - model.php
@@ -116,66 +128,78 @@
116
128
 
117
129
  <?php
118
130
 
119
-
131
+ Class Db{
132
+
120
-
133
+ private static $statement='INSERT INTO comments SET comment=?, created_at=NOW()';
134
+
135
+ private static $comments='SELECT * FROM comments ORDER BY id DESC LIMIT ?,5';
136
+
137
+ private static $counts='SELECT COUNT(*) AS cnt FROM comments';
138
+
139
+ private static $pdo;
140
+
141
+ public function __construct(){
142
+
121
- try{
143
+ try{
122
-
144
+
123
- $db=new PDO('mysql:dbname=keijiban1;host=127.0.0.1;charset=utf8','root','root');
145
+ self::$pdo = new PDO('mysql:dbname=keijiban1;host=127.0.0.1;charset=utf8','root','root');
124
-
125
-
126
-
127
-
128
-
146
+
147
+
148
+
129
- }catch(PDOException $e){
149
+ }catch(PDOException $e){
130
-
150
+
131
- echo 'DB接続エラー:'.$e->getMessage();
151
+ echo 'DB接続エラー:'.$e->getMessage();
152
+
153
+ }
154
+
155
+ }
156
+
157
+ //コメントをデータベースに登録
158
+
159
+ public static function inputdb(){
160
+
161
+ $stmt=self::$pdo->prepare(self::$statement);
162
+
163
+ $stmt->execute(array($_POST['comment']));
164
+
165
+ }
166
+
167
+ //データベースからコメント取り出し
168
+
169
+ public static function outputdb(){
170
+
171
+ $stmt=self::$pdo->prepare(self::$comments);
172
+
173
+ self::$comments->bindParam(1,$start,PDO::PARAM_INT);
174
+
175
+ self::$comments->execute();
176
+
177
+ }
178
+
179
+
180
+
181
+ public static function countdb(){
182
+
183
+ $stmt=self::$pdo->query(self::$counts);
184
+
185
+ $count=self::$counts->fetch();
186
+
187
+
132
188
 
133
189
  }
134
190
 
135
191
 
136
192
 
137
- function inputdb(){
193
+
138
-
139
-
140
-
141
- global $db;
142
-
143
- $statement=$db->prepare('INSERT INTO comments SET comment=?, created_at=NOW()');
144
-
145
- $statement->execute(array($_POST['comment']));
146
194
 
147
195
  }
148
196
 
149
197
 
150
198
 
151
- function outputdb(){
152
-
153
-
154
-
155
- global $db,$comments,$start;
156
-
157
- $comments=$db->prepare('SELECT * FROM comments ORDER BY id DESC LIMIT ?,5');
158
-
159
- $comments->bindParam(1,$start,PDO::PARAM_INT);
160
-
161
- $comments->execute();
162
-
163
- }
164
-
165
-
166
-
167
- function countdb(){
168
-
169
- global $db,$count;
170
-
171
- $counts=$db->query('SELECT COUNT(*) AS cnt FROM comments');
172
-
173
- $count=$counts->fetch();
174
-
175
- }
176
-
177
199
  ?>
178
200
 
201
+
202
+
179
203
  ```
180
204
 
181
- 調べてみると、クラスを使えば可能と書いてありまた。クラスの使方の基本は勉強たのですが、どういう形で今のものに応用したら良いかが見えてきせん。初心者にもわかりやくヒントをいただけると嬉しいです
205
+ よろくお願します。