Confused with active record |
[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( As you can see it is calling the membership model function 'get_uid'. It is defined as such: Code: function get_uid() I'm not sure what I'm doing wrong. As I said I'm a novice. Any help is appreciated!
[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');
[eluser]Buso[/eluser]
try this: Code: $this->db->where('id',$this->input->post('username')); you can also chain everything: Code: return $this->db->where('id',$this->input->post('username'))->get('users')->row()->id;
[eluser]kaos78414[/eluser]
That didn't work. 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
[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'));
[eluser]kaos78414[/eluser]
Yeah, it looks like this now: Code: function get_uid() 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.
[eluser]Buso[/eluser]
[quote author="kaos78414" date="1273919470"]Yeah, it looks like this now: Code: function get_uid() 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
[eluser]kaos78414[/eluser]
Woot! Finally got it! Thank you for being patient with me. This is the end result: Code: function get_uid() Turns out part of it was just syntax, the other was just a little confusion with all the different active record stuff.
[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?
[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. |
Welcome Guest, Not a member yet? Register Sign In |