Welcome Guest, Not a member yet? Register   Sign In
Confused with active record
#1

[eluser]kaos78414[/eluser]
I'm a beginner so this may seem like a silly question but:

I'm trying to pull userid from a database table to store in the session (in order to insert it into other tables later from the session).

So i have this in the controller:
Code:
$data = array(
                'username' => $this->input->post('username'),
                'userID' => $this->membership_model->get_uid(),
                'is_logged_in' => true
            );
            
            $this->session->set_userdata($data);
            redirect('dashboard');

As you can see it is calling the membership model function 'get_uid'. It is defined as such:
Code:
function get_uid()
    {
        $this->db->where('id =', $this->input->post('username'));
        $query = $this->db->get('users');

        return $query->result();
        
    }

I'm not sure what I'm doing wrong. As I said I'm a novice. Any help is appreciated! Smile
#2

[eluser]kaos78414[/eluser]
Btw, it works to store into the session, it just doesn't store the correct value. It returns 'Array', rather then the actual ID number when I use echo $this->session->userdata('userID');
#3

[eluser]Buso[/eluser]
try this:
Code:
$this->db->where('id',$this->input->post('username'));
$query = $this->db->get('users');
$user = $query->row();
return $user->id;
http://codeignitor.com/user_guide/database/results.html

you can also chain everything:

Code:
return $this->db->where('id',$this->input->post('username'))->get('users')->row()->id;
#4

[eluser]kaos78414[/eluser]
That didn't work. Sad I'll try some more solutions according to that part of the user guide.

That returns errors:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/membership_model.php

Line Number: 34



A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\game\system\libraries\Exceptions.php:166)

Filename: libraries/Session.php

Line Number: 662



A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\game\system\libraries\Exceptions.php:166)

Filename: helpers/url_helper.php

Line Number: 541
#5

[eluser]Buso[/eluser]
that's because there are no results for your query

a way of checking that is with $query->num_rows()

Code:
$this->db->where('id',$this->input->post('username'));
$query = $this->db->get('users');

if($query->num_rows()>0) {
  return $query->row()->id;
}

return FALSE;
#6

[eluser]kaos78414[/eluser]
Yeah, it looks like this now:

Code:
function get_uid()
    {
        $this->db->where('id',$this->input->post('username'));
        $query = $this->db->get('users');
        
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row['id'];
        }
        
    }

That is functioning, but its still returning no value to 'userID' in the controller. I don't know why its doing that, there's only one entry in the users table and it should match up. Maybe if I add in another where statement at the top.
#7

[eluser]Buso[/eluser]
[quote author="kaos78414" date="1273919470"]Yeah, it looks like this now:

Code:
function get_uid()
    {
        $this->db->where('id',$this->input->post('username'));
        $query = $this->db->get('users');
        
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row['id'];
        }
        
    }

That is functioning, but its still returning no value to 'userID' in the controller. I don't know why its doing that, there's only one entry in the users table and it should match up. Maybe if I add in another where statement at the top.[/quote]

row() returns an object, you are treating it as an array

if you need an array you can use row_array()

when you wanna know what's going on, just print_r everything

grab the value returned, print_r($row), and see what's in there
#8

[eluser]kaos78414[/eluser]
Woot! Finally got it! Thank you for being patient with me. This is the end result:

Code:
function get_uid()
    {
        $this->db->select('id', 1);
        $this->db->where('username',$this->input->post('username'));
        $query = $this->db->get('users');
        
        if ($query->num_rows() == 1) {
            return $query->row()->id;
        }
        
        return FALSE;
        
    }

Turns out part of it was just syntax, the other was just a little confusion with all the different active record stuff. Big Grin
#9

[eluser]kaos78414[/eluser]
Quick question, is it better to store information like the users id in the session or just call that function each time I need it?
#10

[eluser]WanWizard[/eluser]
If you need the user_id as an indication that a user is logged in, storing the id in the session seems like the obvious thing to do. That would allow you to restore the logged-in state when the user refreshes the page.




Theme © iAndrew 2016 - Forum software by © MyBB