Welcome Guest, Not a member yet? Register   Sign In
Not Connecting to Database


Im getting invalid username/password when login and I believe the reason is because login/register not connecting to database. Here is code within controllers:


class Form extends CI_Controller {

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


               $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[50]');
                $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[2]');
                $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

                if ($this->form_validation->run() == FALSE)


defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct() {
        //load the required libraries and helpers for login
        //load the Login Model
        $this->load->model('LoginModel', 'login');

    public function index() {
        //check if the user is already logged in
        $logged_in = $this->session->userdata('logged_in');
            //if yes redirect to welcome page
        //if not load the login page

  public function loginUser() {


        if ($this->form_validation->run() == FALSE) {


        } else {

            $reslt = $this->Model_user->checkLogin();

            if ($reslt != false) {

                //set session
                $username = $_POST['username'];
                $password = sha1($_POST['password']);

                //fetch from databse
                $this->db->where(array('username' => $username , 'password' => $password));
                $query = $this->db->get('citable')->result_array();

                $user = $query->row();

                //if use exists
                if ($user->username) {

                    //login message
                    $this->session->set_flashdata("success","You are logged in");

                    //set session variables
                    $_SESSION['user_logged'] = TRUE;
                    $_SESSION['username'] = $user->username;



            } else {

                //wrong credentials
                $this->session->set_flashdata('error','Username or Password invalid!');


    //logging out of a user
    public function logoutUser() {

Im slowly getting to goal but keeping getting minor snags. Can anyone help?

Heart Heart ,


Is your database configurations correct in the database config file?

(05-14-2019, 06:34 PM)php_rocs Wrote: @Mekaboo,

Is your database configurations correct in the database config file?

Yep its correct, I contacted webhost to make sure. I chose test some different code for login and now I get this error. Ive gotten this before:

A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home4/cultured/public_html/application/controllers/Login.php:25)
Filename: core/Common.php
Line Number: 564
A PHP Error was encountered
Severity: Error
Message: Call to a member function row() on a non-object
Filename: controllers/Login.php
Line Number: 25

However I got it for this line of code:
$user = $this->db->get_where('user',['email' => $email])->row();

My rows within my DB are:username, email, password.

Trying to understand what I need to change in this line to make it work properly. Thank you for the help Blush

The error like I mentioned before is because your trying to output data to the
view but it has already been outputted to the view.

Look at your code and see where your trying to output your data to the view.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )


What have you done in your trouble shooting to help you find the issue? Do you use Xdebug? Have you tried commenting out certain statements?

Hey folks!

Looking at an old post from here I figured the issue within this code

$user = $this->db->get_where('user',['email' => $email])->row();

I didnt change the word 'user' to my table name in my DB. Now its working decent. I need to now connect my register page to to my DB. I think Ill just copy code from one controller to another.

Thanks for the help, it means alot!!

Theme © iAndrew 2016 - Forum software by © MyBB