Welcome Guest, Not a member yet? Register   Sign In
Autoload Database not working
#1

[eluser]AntonioAntar[/eluser]
Hello friends.

I have been trying to connect my database (phpMyAdmin) to the website I'm working on

I'm trying to set up a login form. I'm having a problem retrieving data from the database (I'm trying to retrieve login credientals from a table).

So this is what I have as my set up for the database from my database.php file

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

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:1337',
'username' => 'root',
'password' => '',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

The database name i want to retrieve the data from is called test and my localhost is localhost:1337

Next step would be autoloading the database and this is where my problems start to happen. This is what I have as default in my autoload.php file

Code:
$autoload['libraries'] = array();
However, when I autoload the database like this

Code:
$autoload['libraries'] = array('database');
My localhost does not load anymore when I refresh it. I'm not getting an error, in fact I'm not getting anything at all, when I refresh it just shows me the page is refreshing and does that forever

When I remove 'database' from array i load the localhost and everything is working again

Now I tried to load the database in my model like this

Code:
$this->load->database();
the localhost refreshes and site works as normal, but my database is not loaded and my data is not retrieved.

Friends, any help would be greatly appreciated

Thank you very much

AntonioAntar
#2

[eluser]treenef[/eluser]
I'm using mamp and my port number is 8888.

But in my database config file I leave it as just 'localhost'

In my default site url I specify the port there

eg

$config['base_url'] = 'http://localhost:8888/mysite/';
#3

[eluser]AntonioAntar[/eluser]
This is also what I have for my config

Code:
$config['base_url'] = 'http://localhost:1337/mysite/';
I changed the localhost:1337 to just localhost in my database.php file. The site is now loading, but the data is still not being retrieved from database.

What I have is a login portal. I want the users to input their email and password, and then I want the controller to check the database to see if the email and password exist, and if they do, the website will direct them to the 'members' page

if the email and password do not match, i want the message displayed 'Incorrect username/password'

I have sample emails and passwords stored in database, but when I input the email and password
in the database, the login portal always brings me back to the login page


This is my code

For my controller I have

Code:
public function login_validation(){
    
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|trim|xss_clean');
    
    if ($this->form_validation->run()){
        redirect('site/members');
    } else {
        $this->load->view('login-view');
        
    }
}
    
    public function validate_credentials() {
        $this->load->model('model_users');
        if ($this->model_users->can_log_in()){
            return true;
        } else {
            $this->form_validation->set_message('validate_credentials','Incorrect username/password.');
                    return false;
        }
    }

The part that isn't working in this code seems to be the validate_credentials part. I input the correct email and password, but the browser does not take me to the 'members page'

This is what I have for my model

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

class Model_users extends CI_Model {
    
    Public Function can_log_in() {
        $this->load->database();
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password',($this->input->post('password')));
        $query = $this->db->get('users');
        
        if ($query->num_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }
}

The table I'm trying to pull the email and password from is called users
#4

[eluser]treenef[/eluser]
So is this a load database issue or an issue with your code?

Eliminate one by doing a simple select statement. If you can do a simple select statement this has nothing to do with autoloading the database.

If it is your code that is an issue I can't help. You'll have to debug it.

Just looking at it I would pass the email and password as parameters to your model

eg.

load_model->checklogin($email,$password)

That should fix it.




Theme © iAndrew 2016 - Forum software by © MyBB