You are here

Database abstraction layer in Drupal 4

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/database.api.php \database
  2. 5 includes/database.inc \database
  3. 6 includes/database.inc \database
  4. 7 includes/database/database.inc \database
  5. 9 core/lib/Drupal/Core/Database/database.api.php \database

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


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

one would instead call the Drupal functions:

$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().

File

includes/database.inc, line 8
Wrapper for database interface code.

Functions

Namesort descending Location Description
db_affected_rows includes/database.mysqli.inc Determine the number of rows changed by the preceding query.
db_affected_rows includes/database.mysql.inc Determine the number of rows changed by the preceding query.
db_affected_rows includes/database.pgsql.inc Determine the number of rows changed by the preceding query.
db_check_setup includes/database.pgsql.inc Verify if the database is set up correctly.
db_connect includes/database.mysqli.inc Initialise a database connection.
db_connect includes/database.mysql.inc Initialize a database connection.
db_connect includes/database.pgsql.inc Initialize a database connection.
db_decode_blob includes/database.mysqli.inc Returns text from a Binary Large OBject value.
db_decode_blob includes/database.mysql.inc Returns text from a Binary Large Object value.
db_decode_blob includes/database.pgsql.inc Returns text from a Binary Large OBject value. In case of PostgreSQL decodes data after select from bytea field.
db_distinct_field includes/database.mysqli.inc Wraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.
db_distinct_field includes/database.mysql.inc Wraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.
db_distinct_field includes/database.pgsql.inc Wraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.
db_encode_blob includes/database.mysqli.inc Returns a properly formatted Binary Large Object value.
db_encode_blob includes/database.mysql.inc Returns a properly formatted Binary Large OBject value.
db_encode_blob includes/database.pgsql.inc Returns a properly formatted Binary Large OBject value. In case of PostgreSQL encodes data for insert into bytea field.
db_error includes/database.mysqli.inc Determine whether the previous query caused an error.
db_error includes/database.mysql.inc Determine whether the previous query caused an error.
db_error includes/database.pgsql.inc Determine whether the previous query caused an error.
db_escape_string includes/database.mysqli.inc Prepare user input for use in a database query, preventing SQL injection attacks.
db_escape_string includes/database.mysql.inc Prepare user input for use in a database query, preventing SQL injection attacks.
db_escape_string includes/database.pgsql.inc Prepare user input for use in a database query, preventing SQL injection attacks. Note: This function requires PostgreSQL 7.2 or later.
db_escape_table includes/database.inc Restrict a dynamic tablename to safe characters.
db_fetch_array includes/database.mysqli.inc Fetch one result row from the previous query as an array.
db_fetch_array includes/database.mysql.inc Fetch one result row from the previous query as an array.
db_fetch_array includes/database.pgsql.inc Fetch one result row from the previous query as an array.
db_fetch_object includes/database.mysqli.inc Fetch one result row from the previous query as an object.
db_fetch_object includes/database.mysql.inc Fetch one result row from the previous query as an object.
db_fetch_object includes/database.pgsql.inc Fetch one result row from the previous query as an object.
db_lock_table includes/database.mysqli.inc Lock a table.
db_lock_table includes/database.mysql.inc Lock a table.
db_lock_table includes/database.pgsql.inc Lock a table. This function automatically starts a transaction.
db_next_id includes/database.mysqli.inc Return a new unique ID in the given sequence.
db_next_id includes/database.mysql.inc Return a new unique ID in the given sequence.
db_next_id includes/database.pgsql.inc Return a new unique ID in the given sequence.
db_num_rows includes/database.mysqli.inc Determine how many result rows were found by the preceding query.
db_num_rows includes/database.mysql.inc Determine how many result rows were found by the preceding query.
db_num_rows includes/database.pgsql.inc Determine how many result rows were found by the preceding query.
db_prefix_tables includes/database.inc Append a database prefix to all tables in a query.
db_query includes/database.inc Runs a basic query in the active database.
db_queryd includes/database.inc Debugging version of db_query().
db_query_range includes/database.mysqli.inc Runs a limited-range query in the active database.
db_query_range includes/database.mysql.inc Runs a limited-range query in the active database.
db_query_range includes/database.pgsql.inc Runs a limited-range query in the active database.
db_query_temporary includes/database.mysqli.inc Runs a SELECT query and stores its results in a temporary table.
db_query_temporary includes/database.mysql.inc Runs a SELECT query and stores its results in a temporary table.
db_query_temporary includes/database.pgsql.inc Runs a SELECT query and stores its results in a temporary table.
db_result includes/database.mysqli.inc Return an individual result field from the previous query.
db_result includes/database.mysql.inc Return an individual result field from the previous query.
db_result includes/database.pgsql.inc Return an individual result field from the previous query.
db_rewrite_sql includes/database.inc Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.
db_set_active includes/database.inc Activate a database for future queries.
db_unlock_tables includes/database.mysqli.inc Unlock all locked tables.
db_unlock_tables includes/database.mysql.inc Unlock all locked tables.
db_unlock_tables includes/database.pgsql.inc Unlock all locked tables. This function automatically commits a transaction.
pager_query includes/pager.inc Perform a paged database query.
tablesort_sql includes/tablesort.inc Create an SQL sort clause.
_db_query includes/database.mysqli.inc Helper function for db_query().
_db_query includes/database.mysql.inc Helper function for db_query().
_db_query includes/database.pgsql.inc Helper function for db_query().
_db_query_callback includes/database.inc Helper function for db_query().
_db_rewrite_sql includes/database.inc Helper function for db_rewrite_sql.

Constants

Namesort descending Location Description
DB_QUERY_REGEXP includes/database.inc