Monday, May 27, 2013

SELECT COUNT(*) using Drupal db_select

Here's how to use Drupal's db_select function to perform a SQL equivalent of:

SELECT COUNT(uid) FROM users WHERE name = $username



Drupal db_select version:

$q = db_select('users', 'u');
$q->fields('u', array('uid'));
$q->condition('u.name', $username);
  
$row_count = $q->countQuery()->execute()->fetchField();

No comments: