CodeIgniter Forums

Full Version: Database retrieval
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]richzilla[/eluser]
Hi all, im having a bit of trouble retrieving data from a database. I was wondering if somebody could point out where im going wrong. Its for user authentication system, and i know there are pre built systems out there, but i thought id have a go myself first.

the error im getting in my view is:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/site.php

Line Number: 24
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/site.php

Line Number: 24
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/richard/webroot/CRUD/system/libraries/Exceptions.php:164)

Filename: libraries/Session.php

Line Number: 662

my controller:

Code:
<?php
    class Site extends Controller {
    
        function index() {
            $this->load->view('site_view');
        }
        
        function login() {
            $this->form_validation->set_rules('username','User Name', 'required');
            $this->form_validation->set_rules('password','Password','required');
            
            $data = array (
            'userName' => $this->input->post('username'),
            'userPass' => $this->input->post('password')
            );
            
            if ($this->form_validation->run() == FALSE) {
                $this->index();
            }
            else {
                $res = $this->user_model->checklog($data);
                
                if ($res != 0) {
                    $this->session->set_userdata(array('loggedIn' => TRUE, 'userId' => $res->id, 'userName' => $res->userName));
                    $this->secure();
                }
                else {
                    $err = array (
                    'error' => "The entered credentials were incorrect"
                    );
                    $this->load->view('site_view', $err);
                }
            }
        
        }
        
        function secure() {
            $data = array (
            'uName' => $this->session->userdata('userName')
            );
            
            $this->load->view('secure_page', $data);
        
        }
    
    }

and my model if it will help:

Code:
<?php
    class User_model extends Model {
        function checklog($data) {
            $res = $this->db->query("SELECT * FROM users WHERE userName = '".$data['userName']."' AND userPass ='". $data['userPass']."'");
            if ($res->num_rows() != 0)
            {
                return $res->result();
            }
            else
            {
                return 0;
            }
        }
        
    }

any help would be appreciated.

thanks

El Forum

[eluser]Dam1an[/eluser]
Well, the obvious thing to check, is that you're actually definatly returning something, put some echo statements in the if/else blocks, and do a var_dump on what it returns in the controller

El Forum

[eluser]richzilla[/eluser]
when i run the sql directly on the database it returns the expected value, so it seems to be retrieving the correct values.