Community Documentation

Helpers Database Functions

Last updated July 6, 2009. Created by NancyDru on January 24, 2008.
Edited by bryan kennedy. Log in to edit this page.

Functions

db_placeholders

This is a new feature in 6.x that enables more secure database code. The Helpers module backports this functionality for Drupal 5.x users. See the API page for more information.

db_result_array($result)

See http://drupal.org/node/206956 for the original feature request, which has been rejected due to the new database support.

Get an array of all the results of a query. This is very handy for building an option list for a form element.

  • Calling parameters
  • $result
    A query result resource from which to extract the array.
  • Return
  • An array containing the results of the query as follows.

    >

    Number of fields in query Returned array
    1 key and value equal to the retrieved field contents.
    2 key equal to the first field and value equal to the second field.
    3 or more key equal to the first field and value as an array containing the remaining fields.

db_insert($table, $fields)

Run an insert query on the active database.

  • Calling parameters
  • $table
    The database table on which to run the insert query.
    $fields
    An associative array of the values to insert. The keys are the fields, and the corresponding values are the values to insert.
  • Return
  • A database query result resource, or FALSE if the query was not executed correctly.

db_update($table, $fields, $where, $where_type='AND')

Run an update query on the active database.

  • Calling parameters
  • $table
    The database table on which to run the insert query.
    $fields
    An associative array of the values to insert. The keys are the fields, and the corresponding values are the values to insert.
    $where
    The where rules for this update query.
    $where_type
    Whether to AND or OR the where rules together.
  • Return
  • A database query result resource, or FALSE if the query was not executed correctly.

db_delete($table, $where, $where_type='AND')

  • Calling parameters
  • $table
    The database table on which to run the insert query.
    $where
    The where rules for this update query.
    $where_type
    Whether to AND or OR the where rules together.
  • Return
  • A database query result resource, or FALSE if the query was not executed correctly.

db_where_clause($where, $where_type='AND', $where_keyword=TRUE)

Build the WHERE portion of an SQL query, based on the specified values.

  • Calling parameters
  • $where
    Associative array of rules in the WHERE clause. If a key in the array is numeric, the value is taken as a literal rule. If it is non-numeric, then it is assumed to be a field name and the corresponding value is the value that it must hold.
    $where_type
    Whether to AND or OR the where rules together.
  • Return
  • An array containing the where clause with sprintf() markers, and an array of values to substitute for them.

As an example, this $where clause would be translated as follows:

$where = array('name'=>'foo', 'type'=>'page', 'created < 1147567877')

becomes:

WHERE (name='foo') AND ('type'='page') AND (created < 1147567877')

db_fetch_all_as_objects($query)

Returns an array with all objects from a query. This function does not do a rewrite_sql for you.

  • Calling parameters
  • $query
    A database query string.
  • Return
  • An array containing an object for each row returned by the query.

db_fetch_all_as_arrays($query)

Returns an array with all arrays from a query. This function does not do a rewrite_sql for you.

  • Calling parameters
  • $query
    A database query string.
  • Return
  • An array containing an array for each row returned by the query.

db_fetch_column($query)

Returns the first column of a query result as an array. Does not do a rewrite sql for you! Call similar to db_query().

  • Calling parameters
  • $query
    A database query string.
  • Return
  • An array containing the first column for each row returned by the query.

db_fetch_assoc($query)

Returns an associative array, with the first column of the result set as the key and the second as the value. This function does not do a rewrite_sql for you. Call similar to db_query().

  • Calling parameters
  • $query
    A database query string.
  • Return
  • An array containing with the first column of the result set as the key and the second as the value for each row returned by the query.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Develop for Drupal

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.