function drush_db_select

8.0.x dbtng.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
6.x dbtng.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
7.x dbtng.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
3.x drush.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
4.x drush.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
5.x dbtng.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')
master dbtng.inc drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC')

A db_select() that works for any version of Drupal.

Parameters

$table: String. The table to operate on.

$fields: Array or string. Fields affected in this operation. Valid string values are '*' or a single column name.

$where: String. WHERE snippet for the operation. It uses named placeholders. see @_drush_replace_query_placeholders()

$args: Array. Arguments for the WHERE snippet.

$start: Int. Value for OFFSET.

$length: Int. Value for LIMIT.

$order_by_field: String. Database column to order by.

$order_by_direction: ('ASC', 'DESC'). Ordering direction.

Return value

A database resource.

Related topics

5 calls to drush_db_select()
drush_core_watchdog_show_many in commands/core/watchdog.drush.inc
Print a table of watchdog messages.
drush_core_watchdog_show_one in commands/core/watchdog.drush.inc
Print a watchdog message.
drush_get_named_extensions_list in commands/core/drupal/environment_7.inc
Return a list of extensions from a list of named extensions. Both enabled and disabled/uninstalled extensions are returned.
drush_module_list in commands/core/drupal/environment_7.inc
Returns a list of enabled modules.
drush_variable_like in commands/core/variable.drush.inc

File

includes/dbtng.inc, line 78
Wrappers to abstract database operations from Drupal version.

Code

function drush_db_select($table, $fields = '*', $where = NULL, $args = NULL, $start = NULL, $length = NULL, $order_by_field = NULL, $order_by_direction = 'ASC') {
  if (drush_drupal_major_version() >= 7) {
    if (!is_array($fields)) {
      if ($fields == '*') {
        $fields = array();
      }
      else {
        $fields = array($fields);
      }
    }
    $query = db_select($table, $table)->fields($table, $fields);
    if (!empty($where)) {
      $query = $query->where($where, $args);
    }
    if (isset($order_by_field)) {
      $query = $query->orderBy($order_by_field, $order_by_direction);
    }
    if (isset($length)) {
      $query = $query->range($start, $length);
    }
    return $query->execute();
  }
  else {
    if (is_array($fields)) {
      $fields = implode(', ', $fields);
    }
    $query = "SELECT $fields FROM {{$table}}";
    if (!empty($where)) {
      $where = _drush_replace_query_placeholders($where, $args);
      $query .= " WHERE " . $where;
    }
    if (isset($order_by_field)) {
      $query .= " ORDER BY $order_by_field $order_by_direction";
    }
    if (isset($length)) {
      $sql = drush_sql_get_class();
      $db_scheme = $sql->scheme();
      if ($db_scheme == 'oracle') {
        return db_query_range($query, $start, $length);
      }
      else {
        $limit = " LIMIT $length";
        if (isset($start)) {
          $limit .= " OFFSET $start";
        }
        $query .= $limit;
      }
    }

    return db_query($query, $args);
  }
}