Welcome Guest, Not a member yet? Register   Sign In
My variable return a OBJECT?
#1

[eluser]zen333[/eluser]
Hi guys,

I am relatively new to CI and a question to ask. Please take a look at part of my codes.


function isUser($username,$password) //pass two varible username and password to this function
{
$t =$this->table;
$user= $this->db->select()->from($t)->where($t."_username",$username)->where($t."_password",$password)->get()->row();
return $user;


}
// This part of the codes below is in Main.php
$user = $this->session->userdata("user");
echo $user;
echo $user->roles;

//echo $user; actually returns OBJECT and echo $user->roles; return A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/main.php Line Number: 17


//This is a table in my database
users_id username password roles
1 aaa admin 1
2 JJJ JJJ 2

Could some kind soul point out my mistake?

Thanks so much!
#2

[eluser]Dam1an[/eluser]
We would need to see what you're doing when setting the User in the session
What do you get if you do print_r/var_dump on the user object?

Also, please use [ code ] tags in the future
#3

[eluser]mddd[/eluser]
You are checking if the user exists, and getting its row from the database, all in one statement.
If the user/pass is not found, the '->row()' part will not work correctly.
So, you should first check if the user/pass combination exists (and therefore, it is a correct user)
and then get its roles.

Also, the select() part should not be there if you are selecting everything.

To check what's happening, turn on the profiler en check what queries are actually perfomed.
#4

[eluser]zen333[/eluser]
Thanks for all the reply. The codes are working fine and when I try to move them to another folder. This problem happen. BTW, what is profiler? And I think maybe some configuration is wrong? I already set my database in config.

For the codes, I am trying to check for the user to exist and retrieve the row from the table. Then I need do a check if

<?php if($user->roles == "1" ):?>
<li> &lt;?php echo anchor('/admin/index','Admin');?&gt; </li>
&lt;?php endif;?&gt;

so to display ADMIN icon if the user is a administrator, i.e roles=1......it return an error, that why I try to echo out for troubleshooting.......Sad
#5

[eluser]Dam1an[/eluser]
The profiler lets you see whats going on, what database qieries you're performing etc

what mddd was refering to, is the code in isUser, I would go for something like:
Code:
// user underscore instead of CamelCase
function is_user($username, $password) {
    // I prefer the slightly more verbose AR pattern
    // I also don't bother prefixing the table name to fields, although thats just personal preference
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    
    // Do the query
    $query = $this->db->get('users');
    
    // It's possible we may get no results, if the username/password was wrong
    // so doing ->row() may throw an error, so check we have > 0 rows
    // I use the ternary operator, but can do an if/else
    return ($query->num_rows() > 0) ? $query->row() : false;
}
#6

[eluser]zen333[/eluser]
Hi Dam1an,

Many thanks for your kind help! really appreciated it. I am sorry for the late reply. I will try it, i hope it will work! Thanks,

Regards,
Zen




Theme © iAndrew 2016 - Forum software by © MyBB