php - 致命错误:未捕获的错误:调用 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;
}
}`
解决方案
实际上,query 并不执行 SQL 查询。您应该在调用方法“fetchAll”之前执行它!
public function index($connexion){
$prepare=$connexion->prepare("select * from medicale2");
$prepare->execute();
$res=$prepare->fetchAll(PDO::FETCH_OBJ);
return $res;
}
推荐阅读
- excel - 范围快照作为弹出窗口
- java - Jackson,如何使用原始类型将 json 转换为 Java 类?
- azure - Azure DevOps 部署到本地计算机
- python - 熊猫中 for 循环的矢量化
- c++ - 在c ++中逐行拆分段落
- javascript - how to run a function when context changes in react
- c++ - How do you use the MySQL Connector/C++ and XDevAPI while using XAMPP to run MySQL?
- swift - Passing Index of a selected row to another ViewController
- c# - Azure 媒体服务作业长时间处于处理状态
- c - 为子类添加额外的“方法”