30 September 2014

This snippet shows you how to get a single query result as string in Drupal 7, using select from database abstraction layer.

Source code viewer
  1. $uid = reset(db_select('user', 'u')
  2. ->condition('u.username', $username)
  3. ->fields('u', array('uid'))
  4. ->range(0, 1)
  5. ->execute()
  6. ->fetchCol());
  7.  
  8. // Now that you have a single value, you can use it to load an entity for an example.
  9. $user = user_load($uid);
Programming Language: PHP