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.title, n.body, 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.title, n.body, 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().

Functions

NameLocationDescription
db_affected_rowsincludes/database.pgsql.incDetermine the number of rows changed by the preceding query.
db_connectincludes/database.pgsql.incInitialize a database connection.
db_decode_blobincludes/database.pgsql.incReturns text from a Binary Large OBject value.
db_encode_blobincludes/database.pgsql.incReturns a properly formatted Binary Large OBject value.
db_errorincludes/database.pgsql.incDetermine whether the previous query caused an error.
db_escape_stringincludes/database.pgsql.incPrepare user input for use in a database query, preventing SQL injection attacks. Note: This function requires PostgreSQL 7.2 or later.
db_fetch_arrayincludes/database.pgsql.incFetch one result row from the previous query as an array.
db_fetch_objectincludes/database.pgsql.incFetch one result row from the previous query as an object.
db_next_idincludes/database.pgsql.incReturn a new unique ID in the given sequence.
db_num_rowsincludes/database.pgsql.incDetermine how many result rows were found by the preceding query.
db_prefix_tablesincludes/database.incAppend a database prefix to all tables in a query.
db_queryincludes/database.incRuns a basic query in the active database.
db_querydincludes/database.incDebugging version of db_query().
db_query_rangeincludes/database.pgsql.incRuns a limited-range query in the active database.
db_resultincludes/database.pgsql.incReturn an individual result field from the previous query.
db_rewrite_sqlincludes/database.incRewrites node queries.
db_set_activeincludes/database.incActivate a database for future queries.
pager_queryincludes/pager.incPerform a paged database query.
tablesort_sqlincludes/tablesort.incCreate an SQL sort clause.
_db_queryincludes/database.pgsql.incHelper function for db_query().
_db_rewrite_sqlincludes/database.incHelper function for db_rewrite_sql.
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.