100% found this document useful (1 vote)
19K views

Drupal 7 Database API Cheat Sheet

Drupal 7 Database API functions and methods. Including debugging, examples and documentation reference.

Uploaded by

wizzlern
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
19K views

Drupal 7 Database API Cheat Sheet

Drupal 7 Database API functions and methods. Including debugging, examples and documentation reference.

Uploaded by

wizzlern
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

->orderRandom() ->db_or()->condition()->condition()

SELECT
return The called query object. return Condition of OR-ed conditions.
db_select($table, $alias = NULL,
$options = array()) ->union(SelectQueryInterface $query, ->db_and()->condition()->condition()
$table Database table to select from. $type = '') return Condition of AND-ed conditions.
$alias Table alias. $query Query to union.
return New query object. $type Type of union. ->isNull($field)
return New resulting query object.
->isNotNull($field)
$query = db_select('users', 'u')
->fields('u', $field The field to check.
->addExpression($expression, $alias
array('uid', 'name')); return The called query object.
= NULL, $arguments = array())
$result = $query->execute();
$expression Expression string.
->exists(SelectQueryInterface
$alias Expression alias.
->distinct($distinct = TRUE) $select);
$arguments Assoc array of placehoders and
$distinct Flag indicating DISTINCT query. placeholder values. ->notExists(SelectQueryInterface
return The called query object. return Unique expression alias. $select);
$select The query to check.
->fields($table_alias, $fields = ->countQuery() return The called query object.
array()) return New query object.
$table_alias Alias of the table the field PAGER
JOIN
belongs to. ->addTag($tag)
$fields Array of field names. ->extend(‘PagerDefault’)
->join($table, $alias = NULL,
$tag Query identification.
return The called query object. $condition = NULL, $arguments = return New pager extender object.
return The called query object.
array())
->addField($table_alias, $field, $table The table to join with. ->extend(‘PagerDefault’)->limit
->hasTag($tag)
$alias = NULL) $alias Table alias. ($count)
$tag Query identification. $condition Join conditions.
$table_alias Alias of the table the field $count Number of items per page.
return TRUE if condition is met. $arguments Assoc array of placeholders and
belongs to.
$field Field name. placeholder values.
return Unique table alias. SORTABLE TABLE
$alias Field alias. CONDITIONS
return Unique field alias.
->extend(‘TableSort’)
->condition($field, $value = NULL, $query = db_select('users', 'u');
$query->innerJoin('node', 'n', return Table extender object.
->range($start = NULL, $length = $operator = NULL)
'n.uid = u.uid'); return The called query object.
NULL) $field The field to check or the result of $query->addField('u', 'name');
$start First record of the result set. a logic operation (or, and, xor) $query->addField('n', 'title'); ->extend(‘TableSort’)->orderByHeader
$length Max number of records. $value The value to test against. $result = $query->execute(); ($header)
return The called query object. $operator Default: ‘=’ or ‘IN’.
Supported: =, <, >, >=, <=, IN, $header Array with sorting criteria.
NOT IN, LIKE, BETWEEN, IS ->innerJoin ($table, $alias = NULL, return The called query object.
->groupBy($field) $condition = NULL, $arguments =
NULL, IS NOT NULL
$field The field to group by. return The called query object. array()) $header = array(
return The called query object. ->leftJoin ($table, $alias = NULL, array(
$condition = NULL, $arguments = 'data' => t('Title'),
->where($snippet, $args = array())
->orderBy($field, $direction = array()) 'field' => 'n.title',
$snippet Where clause (with placeholders) 'sort' => 'desc'),
'ASC') ->rightJoin ($table, $alias = NULL,
$args Assoc array of placeholders and t('Operations'),
$field The field to order by. placeholder values. $condition = NULL, $arguments =
);
$direction 'ASC' or 'DESC'. array())
return The called query object. See join method.

! March 2011! Drupal-7 database! www.wizzlern.nl/drupal/cheat-sheets


->fetchCol($index = 0) $num_updated = db_update('node')
RESULTS   ->fields(array(
$index Numeric index of the column.
return Array of all records.     'uid' => 5,
->execute($args = array(), $options     'status' => 1,
= array())   ))
return The called query object. INSERT   ->condition('created',
REQUEST_TIME - 3600, '>=')
->fetch($mode = NULL, db_insert($table, $options = array   ->execute();
$cursor_orientation = NULL, ())
$cursor_offset = NULL) $table Database table to insert into.
MERGE
$mode Fetch mode. return New query object.
return Result type specified by $mode. db_merge($table, $options = array())
$nid = db_insert('node')
  ->fields(array( $table Database table to merge into
->fetchObject($class_name = NULL,     'title' => 'Example', return New query object
$constructor_args = array())     'uid' => 1,
$class_name Class type to be returned.     'created' => REQUEST_TIME)) db_merge('role')
Default: stdClass   ->execute();   ->key(array('name' => $name))
return Object of one record.   ->fields(array(
      'weight' => $weight,
->values(array $values)   ))
->fetchAssoc() $values Assoc array of values to insert.   ->execute(); QUERIES
return Associative array of one record. return The called query object.
db_query($query, $args = array(),
->key(array $fields, $values = array
->fetchAllAssoc($key, $fetch = NULL) $nid = db_insert('node') $options = array())
())
  ->fields(array('title', 'uid', Note: Access control is not supported! Query may
$key Field name of the array key $fields Array of fields to match or set.
'created')) not be compatible with other database types.
$fetch Fetch mode Or associative array of fields and
  ->values(array(
(PDO::FETCH_ASSOC, values.
    'title' => 'Example',
PDO::FETCH_NUM, or $values Values to set.
    'uid' => 1, settings.php
PDO::FETCH_BOTH). return The called query object.
    'created' => REQUEST_TIME))
return Associative array of data objects Single database configuration example:
  ->execute();
$databases['default']['default'] =
DELETE array(
->fetchAll($mode = NULL,
->from(SelectQueryInterface $query) 'driver' => 'mysql',
$column_index = NULL, db_delete($table, $options = array
$constructor_arguments = array()) $query Select query to fetch the rows 'database' => 'databasename',
())
that should be inserted. 'username' => 'username',
$mode Fetch mode. See above.
return The called query object. $table Database table to delete from. 'password' => 'password',
return Array of data objects. Depending return New query object. 'host' => 'localhost',
on fetch mode. 'prefix' => '',
UPDATE $num_deleted = db_delete('node') 'collation' =>'utf8_general_ci',
->fetchField($index = 0)   ->condition('nid', 5) );
db_update($table, $options = array
$index Numeric index of the column.   ->execute();
())
return A single field.
$table Database table to update. DEBUGGING
return New query object. TRUNCATE
->fetchAllKeyed($key_index = 0, print($query->__toString());
$value_index = 1) db_truncate($table, $options = array
$key_index Numeric index of the array key. ())
$value_index Numeric index of the array value. DOCUMENTATION
$table Database table to remove.
return Associative array of all records. return New query object. Database API on drupal.org:
http://drupal.org/developing/api/database

! March 2011! Drupal-7 database! www.wizzlern.nl/drupal/cheat-sheets

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy