douzun4443 2017-12-01 20:47
浏览 148
已采纳

SQLite调用成员函数fetch()

i get the following error in readChatMessage method when I run this code. I can not find the error. Do you think this is because of a missing try and catch block as I read somewhere else?

Fatal error: Uncaught Error: Call to a member function fetch() on boolean in /opt/lampp/htdocs/A01/src/Mensa/Data.php:23 Stack trace: #0 /opt/lampp/htdocs/A01/src/Mensa/Data.php(16): Mensa\Data->readChatMessages() #1 /opt/lampp/htdocs/A01/src/Mensa/Application.php(19): Mensa\Data->__construct() #2 /opt/lampp/htdocs/A01/public/index.php(8): Mensa\Application->__construct() #3 {main} thrown in /opt/lampp/htdocs/A01/src/Mensa/Data.php on line 23

Data.php:

<?php
namespace Mensa;

require_once(__DIR__.'/Data/ChatMessage.php');
require_once(__DIR__.'/Data/MealSuggestion.php');
use Mensa\Data\ChatMessage;use Mensa\Data\MealSuggestion;use DateTime;use PDO;
class Data{
    const DATA_DIR = __DIR__.'/../../data';
    const DATABASE_FILE = self::DATA_DIR.'/mensa.sqlite';

    protected $chatMessages;
    protected $mealSuggestions;

    function __construct(){
        $this->db = new PDO('sqlite:'.self::DATABASE_FILE);
        $this->readChatMessages();
        $this->readMealSuggestions();
    }


    protected function readChatMessages(){
        $chatMessages = $this->db->query('SELECT*FROM chatmessage');
        while($chatMessageData=$chatMessages->fetch()){
            ...
        }
    }


    protected function readMealSuggestions(){
        $mealSuggestions =  $this->db->query('SELECT*FROM mealsuggestion');
        while($mealSugesstionData=$mealSuggestions->fetch()){
        ...
        }
    }

 protected $db;   
}
?>
  • 写回答

1条回答 默认 最新

  • doujiang5211 2017-12-01 21:02
    关注

    it means that this failed: $chatMessages = $this->db->query('SELECT*FROM chatmessage');

    so $chatMessages has the value false and as a consequence in the next line $chatMessageData=$chatMessages->fetch() throws an error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作