Skip to content
Advertisement

SQLite3 (PHP Fatal error: Call to a member function exec())

<?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

I’ve been innstall

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

and add conf:

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

I have installed the necessary components and added to the configuration file line, maybe in php.ini need to add something?

Advertisement

Answer

Unless Im missing something your protected $_db may never get set so you cannot call exec() on null. It will never get set if this does not equal true –

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

You should check the logic of your class

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement