first time i have asked for help on a forum for a very long time. Normally find what i need by viewing other posts... Well i'm really confused so need help.

I am trying to use theme('pager') in Drupal 7 to have pagination for my table results i get from a db_select query.

The below query produces an error when i include the line:

$output .= theme('pager');

If i leave this line out i get the expected limited 5 results from my table.

My code:

// Set external databse

// build query to get desired results
$query = db_select('diary', 'n')
->fields('n', array('photo_id', 'photo'))
->orderBy('photo_id', 'DESC');

//Create a new object from extend. The reason for this is that extend() creates a 
new object which wraps the current object (Decorator pattern).
$query = $query->extend('PagerDefault')->limit(5);

// Execute query and add to array
$result = $query->execute();

// empty output var
$output = NULL;

// Loop through results and output each row for chosen columns
foreach($result as $row) {
$output .= $row->photo_id.': '.$row->photo.'<br />';

// Show pagination in output
$output .= theme('pager');

// echo output to screen
echo $output;    

// Set db connection back to default (drupal)

The page renders fine without the theme('pager') line.

With it i get an error:

Additional uncaught exception thrown while handling exception.

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table  
&#039;dbMYDATABASENAME.semaphore&#039; doesn&#039;t exist: SELECT expire, 
value FROM {semaphore} WHERE name = :name; Array ( [:name] =&gt; 
theme_registry:runtime:garland:cache ) in lock_may_be_available() (line 167 
of /homepages/28/d228752694/htdocs/drupal7/includes/lock.inc).

Base table or view not found seems to be the key but i cannot see why including the theme function causes this error. I have a basic Drupal 7 install.

Your help is appreciated!


  • Have you tried adding the $output .= theme('pager'); below the db_set_active();? Also, you could make a view for this pretty easily.
    – sareed
    Jan 26, 2015 at 16:51
  • Works a treat! thanks mate!! That has had me rattle for a while... Jan 26, 2015 at 20:56

1 Answer 1


The error is down to the:

// Show pagination in output 
$output .= theme('pager'); 
// echo output to screen 
echo $output; 

being above db_set_active();

Not sure why this is but it fixes the problem.


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.