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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了