jeudi 25 février 2016

Fatal error with sqlite3 and codeigniter

This is my first time trying to make a website using Codeigniter and SQLite3. I have no issues using mysql but SQLite throws fatal error that I don't understand nor know how to solve. I tried searching online and browsed through lot of threads here on stack, but none have the answer or share the same error as mine.

Fatal error: Call to a member function escapeString() on boolean in C:\xampp\htdocs\elcomass\system\database\drivers\sqlite3\sqlite3_driver.php on line 178 A PHP Error was encountered

Severity: Error

Message: Call to a member function escapeString() on boolean

Filename: sqlite3/sqlite3_driver.php

Line Number: 178

Backtrace:

Here is my database config:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => './elcomass/baza/general_info.db',
    'dbdriver' => 'sqlite3',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

I tried changing the dbdriver to just sqlite but then it throws a Call to undefined function sqlite_open() error. I also tried with escaping the strings before I check the database myself, using codeigniter's $this->db->escape but that did nothing even though the fatal error is about escaping strings.

Controller I use for login.

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form_controller extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('user_login');
    }

    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|callback_check_db');

        if ($this->form_validation->run() == TRUE) 
        {   
            $this->session->set_userdata('logged_in', TRUE);
            $this->session->set_userdata('user_stuff', $try_login);
            redirect('/members_area');
        }
        else
        {
            $this->load->view('header');
            $this->load->view('login_form');
            $this->load->view('footer');
        }

    }

    function check_db()
    {
        extract($_POST);
        $try_login = $this->user_login->login($username, $password);

        if(!$try_login)
        {
            $this->form_validation->set_message('check_db', 'Invalid username or password');
            return FALSE;
        }
        else
        {
            $this->session->set_userdata('logged_in', TRUE);
            $this->session->set_userdata('user_stuff', $try_login);
            redirect('/members_area');
        }

    }

/*  public function new_post()
    {
        $formdata = $this->input->post();
        $postname = $this->input->post('postname');
        $postdesc = $this->input->post('postdesc');
        $postdate = date('Y-m-d');
        echo $formdata;
    }*/
}
?>

Model

<?php

class User_login extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

        function login($username, $password)
        {
            $this->db->select('username, password');            
            $this->db->from('login');
            $this->db->where('username', $this->db->escape($username));
            $this->db->where('password', $this->db->escape($password));
            $this->db->limit(1);

            $query=$this->db->get();

            if ($query->num_rows() == 1) {
                return $query->result_array();
            }
            else
            {
                return false;
            }
        }
    }

?>

Aucun commentaire:

Enregistrer un commentaire