Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Database query limit

Tags:

codeigniter

I'm doing some db querys using ActiveRecord and i need to paginate the results so i do a

$this->db->limit($pPagination['Start'], $pPagination['Length']);

but i don't get any results. Using $this->db->last_query(); it seems that CodeIgniter produces the following SQL

SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0 OFFSET 15

which when i run it inside PHPMyAdmin also returns 0 rows.

But if i modify it an run :

SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0, 15

Then i get the correct results. Any ideea why CodeIgniter generates this SQL and why it doesn't work?

I use CodeIgniter 1.7.3 and MySQL 5.1.41

like image 618
daniels Avatar asked Feb 03 '11 18:02

daniels


1 Answers

Ok, found the issue.

It's

$this->db->limit($pPagination['Length'], $pPagination['Start']);

instead of

$this->db->limit($pPagination['Start'], $pPagination['Length']);

First param is the Length and second is the offset, not the other way around as i thought.

like image 115
daniels Avatar answered Oct 06 '22 06:10

daniels