Welcome Guest, Not a member yet? Register   Sign In
num_rows() problem
#1

[eluser]eldrinofsoalim[/eluser]
Hi guys, I have a problem with this code:
Quote:if ($this->session->userdata('username') == "" ) { echo "Blank"; } else { echo "Not Blank"; }

$query1 = $this->db->get_where('users', array('username' => $this->session->userdata('username')));
print_r ($query1->num_rows());

$query2 = $this->db->get_where('users', array('username' => ""));
print_r ($query2->num_rows());

It produces this output:
Quote:Blank<br />
1 <br />
0

My 'username' cookie is empty or blank (as shown in first line of code). HOWEVER, whenever I use the Active Records get_where() function, where table name is 'users' and where field name 'username' is equal to the username cookie (remember, it's blank), it always returns 1.

Why does it return 1 when there is no blank username in the table?

Hoping for your help.

eldrinofsoalim
#2

[eluser]InsiteFX[/eluser]
Code:
if ($this->session->userdata('username') == "")
{
    echo "Blank";
}
else
{
    echo "Not Blank";
}

$this->db->where('username', $this->session->userdata('username');
$query = $this->db->get('users');

print_r ($query->num_rows());

InsiteFX
#3

[eluser]eldrinofsoalim[/eluser]
Hi InsiteFX,

I tried your code and it still produces the same result.

$query->num_rows(); still produces 1 even though the username cookie is blank there is no blank username in the database.
#4

[eluser]InsiteFX[/eluser]
Did you load the session library?

Code:
$this->load->library('session');

InsiteFX
#5

[eluser]eldrinofsoalim[/eluser]
Yes, I did. It's autoloaded in config.
Quote:$autoload['libraries'] = array('database','session');

I'm using this to check if the username cookie set, and if set, will redirect the user to members section, or if not, redirect him/her to the sign-in page.

Are you also producing the same result?
#6

[eluser]InsiteFX[/eluser]
Is the session userdata being set with the users username?

InsiteFX
#7

[eluser]eldrinofsoalim[/eluser]
Not simultaneously. The userdata is set when the controller calls the validate() function in the sign-in controller, which calls $this->session->set_userdata($newuserdata).

For my code in the initial post, I'm just trying to check if the user is logged in via the session cookie. If logged-in, redirect to members-only controller. If not, redirect to sign-in controller.
#8

[eluser]InsiteFX[/eluser]
When the user login set this in your login method:
Code:
$newdata = array(
    'username' => 'johndoe',
    'email' => '[email protected]',
    'logged_in' => TRUE
);

$this->session->set_userdata($newdata);

Check to see if user is logged_in in your controller:
Code:
if (logged_in() == TRUE)
{
    // the user is logged_in
}
else
{
    // user is not logged_in
}

Add this to auth library if you have one.
Code:
public function logged_in()
{
    if ($this->session->userdata('logged_in') == TRUE)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

You may need the CI Instance for the above!

InsiteFX
#9

[eluser]eldrinofsoalim[/eluser]
Great! That did the trick! Thanks InsiteFX.

Is that the proper way to check if the user is logged-in during a website?

Although, I'm still wondering why it still returned a num_row() when obviously there wasn't any blank username.
#10

[eluser]InsiteFX[/eluser]
It returned 1 for the row because it did find the row in the database.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB