CodeIgniter Forums
Easy way to check existence of a user in DB ? ( Newbie ) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Easy way to check existence of a user in DB ? ( Newbie ) (/showthread.php?tid=18429)



Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-06-2009

[eluser]Unknown[/eluser]
Hi, i'm a CodeIgniter newbie, i'm developing a simple authentication system.

My validation controller (target of login form) looks like this:

Code:
$username = $this->input->post('username');
$password = $this->input->post('password');

// Here starts the code for checking the existence of the username && password...

Is there a simple way to check the existence of that user in that table ? I did not found it on the user guide.

Thanks in advance!

(Sorry for my english)


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-06-2009

[eluser]Dam1an[/eluser]
The easiest way is something like
Code:
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() == 1) {
  // Username and password match
} else {
  // Error
}

You might need to chance variable names of the columns/table


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-06-2009

[eluser]Slowcheetah[/eluser]
For the sake of security be sure to check this article; Handling Passwords In CodeIgniter


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-06-2009

[eluser]Unknown[/eluser]
Dam1an, works like a charm! Thanks!

Slowcheetah ok.. i will check that, thank you too Smile


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-11-2009

[eluser]tekhneek[/eluser]
I could be wrong, but wouldn't this achieve the same thing with less code?
Code:
$query = $this->db->get_where('users', array('username' => $username, 'password' => $password));
if ($query->num_rows() === 1) {
   return $query;
} else {
   return false;
}
I've also noticed that using $this->db->where() is very handy in general, but obviously in situations where conditions are needed say for specific preset rows from a database like:
Code:
function get_categories($featured = false) {
    if ($featured !== false) {
        $this->db->where('featured', true);
    }

    $query = $this->db->get('categories');

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

    return false;
}
etc.


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-11-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="tekhneek" date="1242047286"]I could be wrong, but wouldn't this achieve the same thing with less code?
[/quote]
yes it does.
[quote author="tekhneek" date="1242047286"]
I've also noticed that using $this->db->where() is very handy in general, but obviously in situations where conditions are needed say for specific preset rows from a database like:
etc.[/quote]

That's the primary reason I use CI AR Smile


Easy way to check existence of a user in DB ? ( Newbie ) - El Forum - 05-11-2009

[eluser]Dam1an[/eluser]
[quote author="tekhneek" date="1242047286"]I could be wrong, but wouldn't this achieve the same thing with less code?
Code:
$query = $this->db->get_where('users', array('username' => $username, 'password' => $password));
if ($query->num_rows() === 1) {
   return $query;
} else {
   return false;
}
[/quote]

It would acheive the same with less code, but sometimes its better to be a bit more verbose and a little more clear

I could probably do it all in 1 line of code if I wanted, but if it makes it complicated and difficult to understand, whats the point?
(I know you're example isn't complicated per se, but I'm just making the point)