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.
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!!
Comments
8 responses to “Get first row in Codeigniter return from database query”
[…] post Get first row in Codeigniter return from database query appeared first on Magento Host […]
You guys may check more solution about this tutorial in http://stackoverflow.com/questions/4280235/codeigniter-return-only-one-row
I can’t get out the first row as expect, can you help me? Thanks
I use this code
$query->first_row();
And it just return the empty result? What wrong?
I believe your result set didn’t return any data. Can you check if your sql list return any data or not?
Can you try to print_r the result like this and see?
first_row());
Finally, I got it work well. Last time it doesn’t work because it have no result in query.
It work nice. Thanks for your tutorial