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();


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

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


  1. magentohostsolution

    You guys may check more solution about this tutorial in

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

  3. I use this code


    And it just return the empty result? What wrong?

  4. magentohostsolution

    I believe your result set didn’t return any data. Can you check if your sql list return any data or not?

  5. Can you try to print_r the result like this and see?


  6. Finally, I got it work well. Last time it doesn’t work because it have no result in query.

  7. It work nice. Thanks for your tutorial