前提・実現したいこと
パーフェクトPHPのミニブログの写経をしています。
下記のエラーが発生しており、自分でもなんども該当の部分を見比べてみたのですが、解決できません。
よろしくお願いします。
発生している問題・エラーメッセージ
Fatal error: Uncaught Error: Call to a member function prepare() on bool in C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php:19 Stack trace: #0 C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php(32): DbRepository->execute('select a., u.u...', Array) #1 C:\xampp\htdocs\php-blog.localhost\models\StatusRepository.php(22): DbRepository->fetchAll('select a., u.u...', Array) #2 C:\xampp\htdocs\php-blog.localhost\controllers\StatusController.php(8): StatusRepository->fetchAllPersonalArchivesByUserId(NULL) #3 C:\xampp\htdocs\php-blog.localhost\core\Controller.php(41): StatusController->indexAction(Array) #4 C:\xampp\htdocs\php-blog.localhost\core\Application.php(123): Controller->run('index', Array) #5 C:\xampp\htdocs\php-blog.localhost\core\Application.php(103): Application->runAction('status', 'index', Array) #6 C:\xampp\htdocs\php-blog.localhost\web\index_dev.php(7): Application->run() #7 {main} thrown in C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php on line 19
該当のソースコード
DbRepository.php
php
1<?php 2 3abstract class DbRepository 4{ 5 protected $con; 6 7 public function __construct($con) 8 { 9 $this->setConnection($con); 10 } 11 12 public function setConnection($con) 13 { 14 $this->con = $con; 15 } 16 17 public function execute($sql, $params = array()) 18 { 19 $stmt = $this->con->prepare($sql); //19行目です 20 $stmt->execute($params); 21 22 return $stmt; 23 } 24 25 public function fetch($sql, $params = array()) 26 { 27 return $this->execute($sql, $params)->fetch(PDO::FETCH_ASSOC); 28 } 29 30 public function fetchAll($sql, $params = array()) 31 { 32 return $this->execute($sql, $params)->fetchAll(PDO::FETCH_ASSOC); 33 } 34} 35
UserRepository.php
php
1<?php 2 3class UserRepository extends DbRepository 4{ 5 public function insert($user_name, $password) 6 { 7 $password = $this->hashPassword($password); 8 $now = new DateTime(); 9 10 $sql = "insert into user(user_name, password, created_at) values(:user_name, :password, :created_at)"; 11 $stmt = $this->execute($sql, array( 12 ':user_name' => $user_name, 13 ':password' => $password, 14 ':created_at' => $now->format('Y-m-d H:i:s'), 15 )); 16 } 17 18 public function hashPassword($password) 19 { 20 return sha1($password . 'SecretKey'); 21 } 22 23 public function fetchByUserName($user_name) 24 { 25 $sql = "select * from user where user_name = :user_name"; 26 27 return $this->fetch($sql, array(':user_name' => $user_name)); 28 } 29 30 public function isUniqueUserName($user_name) 31 { 32 $sql = "select count(id) as count from user where user_name = :user_name"; 33 34 $row = $this->fetch($sql, array(':user_name' => $user_name)); 35 if($row['count'] === '0'){ 36 return true; 37 } 38 39 return false; 40 } 41} 42
StatusRepository.php
<?php class StatusRepository extends DbRepository { public function insert($user_id, $body) { $now = new DateTime(); $sql = "insert into status(user_id, body, created_at) values(:user_id, :body, :created_at)"; $stmt = $this->execute($sql, array( ':user_id' => $user_id, ':body' => $body, ':created_at' => $now->format('Y-m-d H:i:s'), )); } public function fetchAllPersonalArchivesByUserId($user_id) { $sql = "select a.*, u.user_name from status a left join user u on a.user_id = u.id where u.id = :user_id order by a.created_at desc"; return $this->fetchAll($sql, array(':user_id' => $user_id)); } }
試したこと
本に書かれている通りのコードを写経しています。
補足情報(FW/ツールのバージョンなど)
php 7.3.8
Xampp v.3.2.4
この質問の投稿者はすでに退会しているため、質問者からの返信がない点にご留意ください。
回答4件
この質問の投稿者はすでに退会しているため、質問者からの返信やベストアンサーの選定がない点にご留意ください。
あなたの回答
tips
プレビュー
