Welcome Guest, Not a member yet? Register   Sign In
need help with returning several columns from database using codeigniter
#1

[eluser]doforumda[/eluser]
Hi, I am new to codeigniter. I am making a login systems using codeigniter. with my current code I am only returning one value which is userid from database. How can I return more than one column from database such as userid, username, firstname, lastname from db.

Here is my code

view_login.php
Code:
<body>

<h1>Login here</h1>
<div id="login_form">
    &lt;?php echo form_open('user/login'); ?&gt;
    <div>
        <label>Username:</label>
        &lt;?php echo form_input(array('id' => 'username', 'name' => 'username')); ?&gt;
    </div>
    <div>
        <label>Password:</label>
        &lt;?php echo form_password(array('id' => 'password', 'name' => 'password')); ?&gt;
    </div>
    &lt;?php echo form_submit(array('id' => 'login', 'name' => 'login'), 'Login'); ?&gt;
    &lt;?php echo validation_errors(); ?&gt;
    &lt;?php echo form_close(); ?&gt;
</div>
&lt;/body&gt;

user.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
    function __construct() {
        parent::__construct();
    }
    public function index()
    {
        $this->login();
    }
    function main_page() {
        echo "You are in! ";
    }
    function login() {
        $this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[50]|xss_clean');
        if($this->form_validation->run() == FALSE) {
            $this->load->view('view_login');
        } else {
            extract($_POST);
            $userid = $this->User_model->check_login($username, $password);
            if(!$userid) {
                $this->session->set_flashdata('login_error',TRUE);
                redirect('user/login');
            } else {
                $logged_in = array('logged_in' => TRUE, 'userid' => $userid);
                $this->session->set_userdata($logged_in);
                redirect('user/main_page');
            }
        }
    }
    function logout() {
        $this->session->sess_destroy();
    }
}

user_model.php
Code:
&lt;?php
class User_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }
    
    function check_login($user,$pass) {
        $pass = sha1($pass);
        $query = "SELECT userid FROM user WHERE username = ? AND password = ?";
        $result = $this->db->query($query, array($user, $pass));
        if($result->num_rows() == 1) {
            return $result->row(0)->userid;
        } else {
            return false;
        }
    }
}
?&gt;

please help
#2

[eluser]Taftse[/eluser]
Hey Dforumda,

Change the user model to
Code:
&lt;?php
class User_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }
    
    function check_login($user,$pass) {
        $pass = sha1($pass);
        $query = "SELECT userid FROM user WHERE username = ? AND password = ?";
        $result = $this->db->query($query, array($user, $pass));
        if($result->num_rows() == 1) {
            return $result->row(0)
        } else {
            return false;
        }
    }
}
?&gt;

I have removed the
Code:
->userid;
from the return part so the function now returns the row object not just the value of userid

Good luck with your site
And Welcome to the comunity

Regards
Taftse
#3

[eluser]doforumda[/eluser]
Taftse thanks for your reply Smile




Theme © iAndrew 2016 - Forum software by © MyBB