简体   繁体   English

SQLite调用成员函数fetch()

[英]SQLite Call to a member function fetch()

i get the following error in readChatMessage method when I run this code. 运行此代码时,在readChatMessage方法中收到以下错误。 I can not find the error. 我找不到错误。 Do you think this is because of a missing try and catch block as I read somewhere else? 您是否认为这是因为我在其他地方阅读时缺少try and catch块吗?

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: 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;   
}
?>

it means that this failed: $chatMessages = $this->db->query('SELECT*FROM chatmessage'); 这意味着失败了: $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 因此$chatMessages的值为false ,因此在下一行$chatMessageData=$chatMessages->fetch()引发错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM