Database abstraction layer

Allow the use of different database servers using the same code base.

Drupal provides a slim database abstraction layer to provide developers with the ability to support multiple database servers easily. The intent of this layer is to preserve the syntax and power of SQL as much as possible, while letting Drupal control the pieces of queries that need to be written differently for different servers and provide basic security checks.

Most Drupal database queries are performed by a call to db_query() or db_query_range(). Module authors should also consider using pager_query() for queries that return results that need to be presented on multiple pages, and tablesort_sql() for generating appropriate queries for sortable tables.

For example, one might wish to return a list of the most recent 10 nodes authored by a given user. Instead of directly issuing the SQL query

<?php

SELECT n.nid, n.title, n.created FROM node n WHERE n.uid = $uid LIMIT 0, 10;

?>

one would instead call the Drupal functions:

<?php

$result = db_query_range('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = %d', $uid, 0, 10);
while ($node = db_fetch_object($result)) {
// Perform operations on $node->body, etc. here.
  }

?>

Curly braces are used around "node" to provide table prefixing via db_prefix_tables(). The explicit use of a user ID is pulled out into an argument passed to db_query() so that SQL injection attacks from user input can be caught and nullified. The LIMIT syntax varies between database servers, so that is abstracted into db_query_range() arguments. Finally, note the common pattern of iterating over the result set using db_fetch_object().

Constants

NameLocationDescription
DB_QUERY_REGEXPcore/includes/database.incIndicates the place holders that should be replaced in _db_query_callback().

Functions

NameLocationDescription
db_affected_rowscore/includes/database.mysql.incDetermine the number of rows changed by the preceding query.
db_check_setupcore/includes/database.pgsql.incVerify if the database is set up correctly.
db_column_existscore/includes/database.mysql.incCheck if a column exists in the given table.
db_connectcore/includes/database.mysql.incInitialize a database connection.
db_decode_blobcore/includes/database.mysql.incReturns text from a Binary Large Object value.
db_distinct_fieldcore/includes/database.incAdds the DISTINCT flag to the supplied query if a DISTINCT doesn't already exist in the query. Returns the altered query.
db_encode_blobcore/includes/database.mysql.incReturns a properly formatted Binary Large OBject value.
db_errorcore/includes/database.mysql.incDetermine whether the previous query caused an error.
db_escape_stringcore/includes/database.mysql.incPrepare user input for use in a database query, preventing SQL injection attacks.
db_escape_tablecore/includes/database.incRestrict a dynamic table, column or constraint name to safe characters.
db_fetch_arraycore/includes/database.mysql.incFetch one result row from the previous query as an array.
db_fetch_objectcore/includes/database.mysql.incFetch one result row from the previous query as an object.
db_is_activecore/includes/database.incReturns a boolean depending on the availability of the database.
db_lock_tablecore/includes/database.mysql.incLock a table.
db_placeholderscore/includes/database.incGenerate placeholders for an array of query arguments of a single type.
db_prefix_tablescore/includes/database.incAppend a database prefix to all tables in a query.
db_query_rangecore/includes/database.mysql.incRuns a limited-range query in the active database.
db_query_temporarycore/includes/database.mysql.incRuns a SELECT query and stores its results in a temporary table.
db_resultcore/includes/database.mysql.incReturn an individual result field from the previous query.
db_rewrite_sqlcore/includes/database.incRewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.
db_set_activecore/includes/database.incActivate a database for future queries.
db_status_reportcore/includes/database.mysql.incReport database status.
db_table_existscore/includes/database.mysql.incCheck if a table exists.
db_unlock_tablescore/includes/database.mysql.incUnlock all locked tables.
db_versioncore/includes/database.mysql.incReturns the version of the database server currently in use.
pager_querycore/includes/pager.incPerform a paged database query.
tablesort_sqlcore/includes/tablesort.incCreate an SQL sort clause.
update_sqlcore/includes/database.incPerform an SQL query and return success or failure.
_db_error_pagecore/includes/database.incHelper function to show fatal database errors.
_db_querycore/includes/database.mysql.incHelper function for db_query().
_db_query_callbackcore/includes/database.incHelper function for db_query().
_db_rewrite_sqlcore/includes/database.incHelper function for db_rewrite_sql.