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

質問編集履歴

1

質問内容の詳細を記述

2020/08/05 10:54

投稿

imawork
imawork

スコア6

title CHANGED
@@ -1,1 +1,1 @@
1
- クラスインス変数の基本は学んだのすが、応用方法がわかりません。
1
+ Fatal error: Uncaught Error: Call to a member function execute() on string in 〜と出てデーベーに接続ません。
body CHANGED
@@ -1,6 +1,9 @@
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
+ 以下がcomment.phpと、model.phpのコードです。
4
+
2
5
  - comment.php
3
-  ```php
6
+ ```php
4
7
  <?php
5
8
  require('model.php');
6
9
  require('template.html');
@@ -9,8 +12,8 @@
9
12
 
10
13
  if(isset($_POST['comment'])){
11
14
  print('投稿しました<br>');
15
+ $db=new Db;
12
- inputdb();
16
+ $db->inputdb();
13
-
14
17
  }else{
15
18
 
16
19
  }
@@ -22,10 +25,11 @@
22
25
  $page=1;
23
26
  }
24
27
  $start=5*($page-1);
28
+ $db=new Db;
25
- outputdb();
29
+ $db->outputdb();
26
30
 
27
-
31
+ //コメント、時間を繰り返し表示
28
- while($comment=$comments->fetch()){
32
+ while($comment=self::$comments->fetch()){
29
33
  print('<p>');
30
34
  print($comment['comment']);
31
35
  print('</p>');
@@ -37,55 +41,63 @@
37
41
  }
38
42
 
39
43
 
40
-
44
+ ////2ページ目以降なら前のページを表示する(1ページ目は表示しない)
41
45
  if($page>=2){
42
46
 
43
47
  print('<a href="comment.php?page='.($page-1).'">前へ</a >');
44
48
  }
45
49
 
46
-
50
+ //最終ページは表示コメント数/5の繰り上げで表示
51
+ $db=new Db;
47
- countdb();
52
+ $db->countdb();
48
53
 
49
54
  $max_page=ceil($count['cnt']/5);
50
-
55
+ //現在のページ<最終ページなら次ページを表示する
51
56
  if($page<$max_page){
52
57
  print('<a href="comment.php?page='.($page+1).'">次へ</a>');
53
58
  }
54
59
 
55
60
  ?>
61
+
56
62
  ```
57
63
  - model.php
58
64
  ```php
59
65
  <?php
66
+ Class Db{
67
+ private static $statement='INSERT INTO comments SET comment=?, created_at=NOW()';
68
+ private static $comments='SELECT * FROM comments ORDER BY id DESC LIMIT ?,5';
69
+ private static $counts='SELECT COUNT(*) AS cnt FROM comments';
70
+ private static $pdo;
71
+ public function __construct(){
72
+ try{
73
+ self::$pdo = new PDO('mysql:dbname=keijiban1;host=127.0.0.1;charset=utf8','root','root');
60
74
 
61
- try{
75
+ }catch(PDOException $e){
76
+ echo 'DB接続エラー:'.$e->getMessage();
77
+ }
78
+ }
79
+ //コメントをデータベースに登録
80
+ public static function inputdb(){
81
+ $stmt=self::$pdo->prepare(self::$statement);
62
- $db=new PDO('mysql:dbname=keijiban1;host=127.0.0.1;charset=utf8','root','root');
82
+ $stmt->execute(array($_POST['comment']));
83
+ }
84
+ //データベースからコメント取り出し
85
+ public static function outputdb(){
86
+ $stmt=self::$pdo->prepare(self::$comments);
87
+ self::$comments->bindParam(1,$start,PDO::PARAM_INT);
88
+ self::$comments->execute();
89
+ }
63
90
 
91
+ public static function countdb(){
92
+ $stmt=self::$pdo->query(self::$counts);
93
+ $count=self::$counts->fetch();
64
94
 
65
- }catch(PDOException $e){
66
- echo 'DB接続エラー:'.$e->getMessage();
67
95
  }
68
96
 
69
- function inputdb(){
70
-
71
- global $db;
72
- $statement=$db->prepare('INSERT INTO comments SET comment=?, created_at=NOW()');
73
- $statement->execute(array($_POST['comment']));
74
- }
75
97
 
76
- function outputdb(){
77
-
78
- global $db,$comments,$start;
79
- $comments=$db->prepare('SELECT * FROM comments ORDER BY id DESC LIMIT ?,5');
80
- $comments->bindParam(1,$start,PDO::PARAM_INT);
81
- $comments->execute();
82
98
  }
83
99
 
84
- function countdb(){
85
- global $db,$count;
86
- $counts=$db->query('SELECT COUNT(*) AS cnt FROM comments');
87
- $count=$counts->fetch();
88
- }
89
100
  ?>
101
+
90
102
  ```
91
- 調べてみると、クラスを使えば可能と書いてありまた。クラスの使方の基本は勉強たのですが、どういう形で今のものに応用したら良いかが見えてきせん。初心者にもわかりやくヒントをいただけると嬉しいです
103
+ よろくお願します。