I'm trying the following and it works:


   function my_menu() {
      $items['page_example'] = array(
        'title' => 'Page Example',
        'description' => 'Page Example',
        'page callback' => 'page_example',
        'access callback' => TRUE,
        'file' => 'my.page.inc',
        'file path' => drupal_get_path('module', 'my'),
        'type' => MENU_CALLBACK,
     return $items;


function page_example() {
  return array(
    'page_example' => array(
      '#type' => 'markup',
      '#markup' => generate_list(),

function generate_list() {
  $query = db_select('users', 'name')
    ->fields('name', array('Name'))
    ->orderBy('created', 'DESC')

  $result = $query->execute();
  $output = '';
  foreach ($result as $row) {
    $output .= $row->name.'<br/>';

  $output .= theme('pager');
  return $output;

But then I try prepending a uid-field to each row and it fails:

  $query = db_select('users', 'name', 'uid')
    ->fields('name', array('Name'), 'uid', array('Id'))
    //->addField('uid', 'User Id')
    //->addField('name', 'User Name')
    ->orderBy('created', 'DESC')

  $result = $query->execute();
  $output = '';
  foreach ($result as $row) {
    $output .= $row->uid.': '.$row->name.'<br/>';

With the error:

Recoverable fatal error: Argument 3 passed to db_select() 
must be an array, string given, called in my.page.inc 
on line 13 and defined in function db_select() 
(line 2387 in includes/database/database.inc).


On Camsoft's comment (thank you) I've modified the code to:

  $query = db_select('users')
    ->fields('name', array('Name'), 'uid', array('Id'))
    //->addField('uid', 'User Id')
    //->addField('name', 'User Name')
    ->orderBy('created', 'DESC')

And now it fails with

DOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: 
missing FROM-clause entry for table "name" at character 8: 
SELECT name.Name AS Name, created AS created FROM {users} 
users ORDER BY created DESC LIMIT 20 OFFSET 0; Array ( ) 
in function PagerDefault->execute() (line 80 includes/pager.inc).

I'm just trying to figure out how to add a 2nd SQL-table field to a paginated db_select... Please do not suggest a "View" to me, because I'm trying to understand DBTNG at the moment.


The call to db_select() and the following calls to methods are wrong.

  • The second parameter of db_select() is a table alias, which should then used when referring to the fields used by the query.
  • the parameters passed to the fields() is wrong; see the following snippet for an example of how the call to db_select() should be.
$result = db_select('file_usage', 'f')
   ->fields('f', array('module', 'type', 'id', 'count'))
   ->condition('fid', $file->fid)
   ->condition('count', 0, '>')

Your call to db_select() should be re-written as:

$query = db_select('users', 'u')
  ->fields('u', array('name', 'uid'))
  ->orderBy('created', 'DESC')

Could you not just use the Views module to do this?

When creating a view just select User from the View Type list. It's a lot easier than doing this programmatically.


According to the documentation for db_select the third paramter is supposed to be an $options array.

db_select($table, $alias = NULL, array $options = array())

The clue is in the error message:

Recoverable fatal error: Argument 3 passed to db_select() must be an array, string given, called in my.page.inc

You code passes the string uid as the third parameter.

  • No, because I'm trying to make a paginating view of an external, non-Drupal, PostgreSQL-table and this question is the 1st in a series :-) – Alexander Farber Mar 23 '11 at 16:00
  • wait, but your question says Drupal 7 users... – Chaulky Mar 23 '11 at 16:06
  • Ok, I've updated the title – Alexander Farber Mar 23 '11 at 16:11
  • The PostgresSQL may scupper you, but views can point to external datasources, I think the only limitation is that you can't use different types in the same request. – Jeremy French Mar 23 '11 at 16:19
  • I've updated my question with a new try – Alexander Farber Mar 23 '11 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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