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.

[php]$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
}[/php]

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

[php]$user = $query->row();[/php]

Or

[php]if ($query->num_rows() > 0) {
$user = $query->first_row();
}[/php]

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


Posted

in

,

by

Tags:

Comments

8 responses to “Get first row in Codeigniter return from database query”

  1. […] post Get first row in Codeigniter return from database query appeared first on Magento Host […]

  2. magentohostsolution Avatar
    magentohostsolution

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

  3. Laura Avatar
    Laura

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

  4. Laura Avatar
    Laura

    I use this code

    $query->first_row();

    And it just return the empty result? What wrong?

  5. magentohostsolution Avatar
    magentohostsolution

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

  6. Philip Avatar
    Philip

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

    first_row());

  7. Laura Avatar
    Laura

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

  8. Kitrik Avatar
    Kitrik

    It work nice. Thanks for your tutorial