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

6 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_pm_disable in commands/pm/pm.drush.inc
Command callback. Disable one or more extensions.
drush_pm_enable in commands/pm/pm.drush.inc
Command callback. Enable one or more extensions from downloaded projects. Note that the modules and themes to be enabled were evaluated during the pm-enable validate hook, above.
drush_variable_like in commands/core/variable.drush.inc

... See full list

File

includes/drush.inc, line 2057
The drush API implementation and helpers.

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 (!is_null($order_by_field)) {
      $query = $query->orderBy($order_by_field, $order_by_direction);
    }
    if (!is_null($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 (!is_null($order_by_field)) {
      $query .= " ORDER BY $order_by_field $order_by_direction";
    }
    if (!is_null($length)) {
      $db_spec = _drush_sql_get_db_spec();
      $db_scheme = _drush_sql_get_scheme($db_spec);
      $limit = " LIMIT $length";
      if (!is_null($start)) {
        $limit .= " OFFSET $start";
      }
      $query .= $limit;
    }

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