SQLite3(PHP致命错误:调用成员函数exec())

时间:2015-04-08 20:33:27

标签: php sqlite

<?php
require 'INewsDB.class.php';
class NewsDB implements INewsDB
{
    protected $_db;

    const DB_NAME = '/home/petrov/public_html/mysite/news.db';

    function __construct()
    {
        if (is_file(self::DB_NAME)) {
            $this->_db = new SQLite3(self::DB_NAME);
        } else {
            $sql = "CREATE TABLE msgs(id INTEGER PRIMARY KEY AUTOINCREMENT,
                    title TEXT,
                    category INTEGER,
                    description TEXT,
                    source TEXT,
                    datetime INTEGER)";
            $this->_db->exec($sql) or die($this->_db->lastErrorMsg());//!!!!
            $sql = "CREATE TABLE category(id INTEGER,name TEXT)";
            $this->_db->exec($sql) or die($this->_db->lastErrorMsg());


$news = new NewsDB;
?>
error apache.log:
[Wed Apr 08 23:24:04.918051 2015] [:error] [pid 29353] [client 127.0.0.1:51137] PHP Fatal error:  Call to a member function exec() on a non-object in /home/petrov/public_html/mysite/news/NewsDB.class.php on line 20

我已经是住了

sudo apt-get install php5-sqlite
sudo apt-get install php-sqlite3
sudo apt-get install sqlite

并添加conf:

extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so

我已经安装了必要的组件并添加到配置文件行,也许在php.ini中需要添加一些东西?

1 个答案:

答案 0 :(得分:1)

除非我遗漏了某些内容,否则protected $_db可能永远不会被设置,因此您无法在null上调用exec()。如果不相等,它永远不会被设置 -

if (is_file(self::DB_NAME)) {
     $this->_db = new SQLite3(self::DB_NAME);
} else {

你应该检查你班级的逻辑