douzun4443 2017-12-01 12: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 13: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部