首页 > 解决方案 > 致命错误:未捕获的错误:调用 bool php mvc pdo 上的成员函数 fetchAll()

问题描述

我尝试了以下代码,但出现错误“致命错误:调用 bool 上的成员函数 fetchAll() 这是控制器I have try the following code but the error "Fatal error: Call to a member function fetchAll() on a non-object 这是代码 `<?php 类症状 { private $id_sym,$nom_sym;

public function __construct($id_sym,$nom_sym){
    $this->id_sym=$id_sym;
    $this->nom_sym=$nom_sym;
        
}

public function add($connexion){
    $connexion->exec("insert into medicale2(nom_sym) values('".$this->nom_sym."')");
    header("location:index.php?controller=symptome&action=index");
}

public function edit($connexion){
    
    $connexion->exec("update medicale2 set nom_sym='".$this->nom_sym."'");
    header("location:index.php?controller=symptome&action=index");
}

public function delete($connexion){
    $connexion->exec("delete from medicale2 where id_sym='".$this->id_sym."'");
    header("location:index.php?controller=symptome&action=index");
}

public function index($connexion){
    $res=$connexion->query("select * from medicale2")->fetchAll(PDO::FETCH_OBJ);
    
    return $res;
    
}


public function detail($connexion){
    $res=$connexion->query("select * from medicale2 where id_sym='".$this->id_sym."'")->fetch(PDO::FETCH_OBJ);
    return $res;
}

}`

标签: phpmodel-view-controllerpdo

解决方案


实际上,query 并不执行 SQL 查询。您应该在调用方法“fetchAll”之前执行它!

public function index($connexion){
    $prepare=$connexion->prepare("select * from medicale2");
    $prepare->execute();
    $res=$prepare->fetchAll(PDO::FETCH_OBJ);
    
    return $res;
    
}

推荐阅读