I am trying to develop a simple module in Drupal 7. I want to return a table with some data in a function, which i am able to achieve. I am stuck in the pager part though.

I used

pager_default_initialize($total, 10);
$html .= theme('pager', array('quantity',$total));

and I can see the pager below my list, also I have 21 records for now and it shows << < 1 2 3 > >> which means it calculating 10 records each.

But my issue here is I can see all the 21 records at once regardless of whichever page I go to. I suppose I have goofed up my page_query. When I tried using $query->limit(10) it gives error Fatal error:

Call to undefined method SelectQuery::limit() in C:\xampp\htdocs...............

If I comment $query->limit, there's no error but it does not work completely. This is my query

$query = db_select('profile');
$query->fields('profile', array('id', 'name', 'store_name', 'company', 'city', 'email', 'phone'));
$query->orderBy('id', 'DESC');
$results = $query->execute();

Please help!

Thanks in advance! :)

2 Answers 2


extend() isn't chain-able, it returns a new extended query object, so you need to assign the result back to the query object, e.g.

$query = $query->extend('PagerDefault')->limit(10);

Otherwise the query won't actually be extended and won't have the limit() method, hence the error you're getting.

  • Hi, your solution worked. The error is gone. however now I cannot see the pager. Thx a ton.
    – NewBie
    Jan 14, 2013 at 12:00
  • 3
    Just to add I had to remove pager_default_initialize($total, 10); to make it work. so I used $query = $query->extend('PagerDefault')->limit(10); in the query and $html .= theme('pager', array('quantity',$total)); in my function. Works like a charm. \m/ thanks again for your time..:)
    – NewBie
    Jan 14, 2013 at 12:03

Because you have to use range : db_select documentation and more precisely range.

  • 1
    range() is just for basic limiting, the OP is trying to add a pager to the query
    – Clive
    Jan 14, 2013 at 11:40
  • 1
    I up'd your answer, thx for the info Clive. Jan 14, 2013 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.