Get first row in Codeigniter return from database query

Recently, I has started with a project what connect from Magento to the back end of Codeigniter. Everything with Codeigniter with me is new so that I have to follow step by step in the guide. I usually want to get first row in codeigniter return from database result set instead of a list of rows. Here is will share you how to get it.

Get first row return from query in Codeigniter

Let ‘s say we do the login process and we only need to get out the first user match with requirements.

$this->db->select('*')->from('user')->where('username', $data['username'])->where('password', md5($data['password']));
if($query->num_rows) {
    $user = $query->row_array();
    // process with user information to save to session
} else {
    // login fail process
}

Other than that, you still have some other way to do that

$user = $query->row();

Or

if ($query->num_rows() > 0) {
    $user = $query->first_row();
}

So, it ‘s easy to¬†get first row in codeigniter, right? Let ‘s try it!!

8 comments

  1. magentohostsolution

    You guys may check more solution about this tutorial in http://stackoverflow.com/questions/4280235/codeigniter-return-only-one-row

  2. I can’t get out the first row as expect, can you help me? Thanks

  3. I use this code

    $query->first_row();

    And it just return the empty result? What wrong?

  4. It work nice. Thanks for your tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*