未捕获的错误:在其他 php 文件上创建我自己的 fetch 方法后调用未定义的方法 Database::fetchAll()

问题描述 投票:0回答:0

我目前正在 youtube 上观看来自 laracasts 的 php 初学者播放列表,但由于上图中显示的错误,我已经被困了几天

我卡在了第 24 集,还不能继续看下一个视频

我一直在重放视频和重写代码,但我仍然遇到同样的错误。从教程中我需要在我的 note.php 上制作我自己的获取方法(在这种情况下,它的方法名称是 findOrFail()),这是语法

<?php
 $config = require('config.php');

 $db = new Database($config['database']);



 $heading = "Note" ;
 $currentUserId = 1;



 $notes = $db->query('select * from notes where id = :id', [
'id' => $_GET['id']

])->findOrFail();


if ($note['user_id'] !== $currentUserId){
    abort(Response::FORBIDDEN);
}

require "views/note.view.php"; 

和 notes.php 语法

<?php
 $config = require('config.php');
 $db = new Database($config['database']);



 $heading = "My Notes" ;

 $notes = $db->query('select * from notes where user_id = 1')->fetchAll();



  require "views/notes.view.php"; 

关于 Database.php 的语法,就在这里

<?php 
class Database{
public $connection;

public $statement;
public function __construct($config, $username = 'root', $password=''){


    $dsn = 'mysql:' . http_build_query($config, '', ';'); // example.com?host=localhost;port=3306;dbname=myapp

     

    $this->connection = new PDO($dsn, $username, $password,[
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);   
}
public function query($query, $params = []){

    $this->statement = $this->connection->prepare($query);


    $this->statement->execute($params);
    
    return $this;
}

public function find(){

    return $this->statement->fetch();
}

public function findOrFail(){
        $result = $this->find();

        if(! $result){
        abort();
         }

        return $result;

     }

      }
php pdo
© www.soinside.com 2019 - 2024. All rights reserved.