Drupal 7: Displaying external SQL-table as paginated list The following code works well for displaying a paginated list of Drupal users:

function generate_list() {
  $query = db_select('users', 'u')
    -&gt;extend('PagerDefault')
    -&gt;fields('u', array('name', 'uid'))
    -&gt;orderBy('created', 'DESC')
    -&gt;limit(20);

  $result = $query-&gt;execute();

  $list = array();
  foreach ($result as $row) {
    $list[] = sprintf('&lt;a href="/user.php?id=DE%u"&gt;%s&lt;/a&gt;', $row-&gt;uid, $row-&gt;name);
  }

  $theme_args = array('items' =&gt; $list, 'type' =&gt; 'ul');
  return theme('item_list', $theme_args) . theme('pager');
}


But when I try fetching from an external SQL-table called pref_users (my site embeds a card game and some users come from outside - through Facebook and other social networks, so I have to maintain some non-Drupal SQL-tables, in the same database where Drupal data is stored) with this code:

function generate_list() {
  $query = db_select('pref_users', 'u')
    -&gt;extend('PagerDefault')
    -&gt;fields('u', array('first_name', 'id'))
    -&gt;orderBy('created', 'DESC')
    -&gt;limit(20);

  $result = $query-&gt;execute();

  $list = array();
  foreach ($result as $row) {
    $list[] = sprintf('&lt;a href="/user.php?id=%s"&gt;%s&lt;/a&gt;', $row-&gt;id, $row-&gt;first_name);
  }

  $theme_args = array('items' =&gt; $list, 'type' =&gt; 'ul');
  return theme('item_list', $theme_args) . theme('pager');
}


then I unfortunately get the error:

PDOException: SQLSTATE[42P01]: Undefined table: 7 
ERROR: relation "drupal_pref_users" does not exist at character 67: 
SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression 
FROM {pref_users} u) subquery; Array ( ) in method 
PagerDefault-&gt;execute() (line 75 in file includes/pager.inc).


Does anybody please know a way to tell Drupal to fetch data simply from pref_users and not from {pref_users} at it seems to assume above?
the squiggly brackets are used for escaping table names in drupal. For cases where you have multiple sites in one database.

I'm guessing that you have a db prefix set up for all of your Drupal tables to be drupal_

You can set this on a db connection before $result = $query-&gt;execute(); using the setprefix method

Something like 

 $query-&gt;setprefix(array(
     'default'   =&gt; 'drupal_',
     'perf_users'     =&gt; ''));


untested code

may do it. However you are more likely to want to change your settings.php file and set table prefixs in there